请大家指点一下WHILE语句

adot111 2003-10-20 10:09:36
我在写WHILE语句的时候,好象有些问题,请大家指点

下面我想写的是,如果输入的数字大于3,系统显示GOOD,否则,系统继续要求USER输入一个数字

import javax.swing.*;
public class A{
public static void main(String args[]){
int input2;


boolean Int = false;

while(!false){
String input = JOptionPane.showInputDialog("Enter a integer");
input2 = Integer.parseInt(input);


if(input2 < 3){
Int = false;
}
else{
Int = true;
System.out.println("good"); }

System.exit(0);
}

}
}

...全文
44 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohaiz 2003-10-21
  • 打赏
  • 举报
回复
简单点就行,直接使用while(true),每次询问输入,检查输入,如果输入>3,break.
qiyongjun2003 2003-10-21
  • 打赏
  • 举报
回复
import javax.swing.*;
public class A
{
public static void main(String args[])
{
int input2;
//boolean Int = false;
int Int=0;
while(Int==0)
{
String input = JOptionPane.showInputDialog("Enter a integer");
try{
input2 = Integer.parseInt(input);
if(input2 < 3)
{
Int =0;
}
else
{
Int = 1;
System.out.println("good"); }
}
catch(NumberFormatException e)
{}//校验输入是否为整数
//System.exit(0);放在此处无论输入什么将退出
}
System.exit(0);
}
}
adot111 2003-10-20
  • 打赏
  • 举报
回复
我怎么觉得是应该先输入一个数值,再进行比较呢,象这样:
public class A{
public static void main(String args[]){
int input2, input4;

String input = JOptionPane.showInputDialog("Enter a integer");
input2 = Integer.parseInt(input);

boolean Int = false;
if(input2 < 3){
while(!false){
String input2 = JOptionPane.showInputDialog("Enter a integer");


}

}
else{
Int = true;
System.out.print("good");
}

System.exit(0);


}
}
虽然是不对,但是我总是不知道为什么这样,思路是错误的
adot111 2003-10-20
  • 打赏
  • 举报
回复
谢谢回复,我怎么思路总是理不清呢,请大家指点一下,谢谢
TonyTonyQ 2003-10-20
  • 打赏
  • 举报
回复
import javax.swing.*;
public class A extends JFrame{
public static void main(String args[]){
int input2;


boolean Int = false;

while(!Int){
String input = JOptionPane.showInputDialog("Enter a integer");
input2 = Integer.parseInt(input);


if(input2 < 3){
Int = false;
}
else{
Int = true;
System.out.println("good"); }

}
System.exit(0);

}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧