这是一个猜数字的小程序~~ 在eclipse下运行没反应javaw.exe还很占cpu资源。。求救。。import javax.swing.JOptionPane;public class CH05EX15 {
public static void main(String[] args)
{
//declare the variables
int num,guess,diff;
String inputStr,outputStr;
boolean done;
num=(int)(Math.random()* 100);
done= false;
while(!done);
{
// Input your guess
inputStr = JOptionPane.showInputDialog
("Enter the number you guess.");
guess = Integer.parseInt(inputStr);
diff=num-guess;
if (diff==0)
{
outputStr= "You guessed the correct number.";
done= true;
}
else
if(diff>=50 && diff<=-50)
outputStr= "The guess is very high or very low.";
else
if((50>diff||diff>=30) && (diff <=-30||-50<=diff))
outputStr= "The guess is high or low.";
else
if((30>diff||diff>=15) &&(diff<=-15||-30<=diff))
outputStr= "The guess is moderately.";
else
outputStr="The guess is somewhat high.";
}
JOptionPane.showMessageDialog(null, outputStr,
"Result",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}}
public static void main(String[] args)
{
//declare the variables
int num,guess,diff;
String inputStr,outputStr;
boolean done;
num=(int)(Math.random()* 100);
done= false;
while(!done);
{
// Input your guess
inputStr = JOptionPane.showInputDialog
("Enter the number you guess.");
guess = Integer.parseInt(inputStr);
diff=num-guess;
if (diff==0)
{
outputStr= "You guessed the correct number.";
done= true;
}
else
if(diff>=50 && diff<=-50)
outputStr= "The guess is very high or very low.";
else
if((50>diff||diff>=30) && (diff <=-30||-50<=diff))
outputStr= "The guess is high or low.";
else
if((30>diff||diff>=15) &&(diff<=-15||-30<=diff))
outputStr= "The guess is moderately.";
else
outputStr="The guess is somewhat high.";
}
JOptionPane.showMessageDialog(null, outputStr,
"Result",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}}
import javax.swing.JOptionPane; public class CH05EX15 { public static void main(String[] args){ int num,guess,diff;
String inputStr,outputStr; boolean done; num=(int)(Math.random()* 100); done= false; while(!done){
inputStr = JOptionPane.showInputDialog("Enter the number you guess.");
guess = Integer.parseInt(inputStr);
diff = num - guess; if (diff==0){
outputStr= "You guessed the correct number.";
done= true;
}
else if(Math.abs(diff) >= 50)
outputStr= "The guess is very high or very low.";
else if((Math.abs(diff) < 50)||(Math.abs(diff) >= 30))
outputStr= "The guess is high or low.";
else if((Math.abs(diff) < 30)||(Math.abs(diff) >= 15))
outputStr= "The guess is moderately.";
else
outputStr="The guess is somewhat high.";
JOptionPane.showMessageDialog(null, outputStr, "Result", JOptionPane.INFORMATION_MESSAGE); //把这句移到while循环内
}
System.exit(0);
}
}
2 初始化outputStr=""; 不初始化会出错
3 你的输入界面未考虑用户点取消了你怎么做
你对比一下你的判断条件,测试一下,你把&&、||我感觉用反了。为了便于测试,我把英文改成了汉语,有什么及时提问、交流!