这是一个非常简单的程序。计算机和用户在 0-3 之间选择一个数字。如果用户没有猜出与计算机相同的数字,我希望程序循环返回。
String input; // input is a string variable
int cpucrazy;
int convertstring;
//第一步//
input = JOptionPane.showInputDialog("Guess a number between 0-3");
convertstring = Integer.parseInt(input);
//随机部分//
Random ran = new Random ();
cpucrazy = ran.nextInt(3);
//计算!
if (cpucrazy < convertstring) {
JOptionPane.showInputDialog(null, "Your guess is too high. Guess again?"); }
else if (cpucrazy > convertstring) {
JOptionPane.showInputDialog(null, "Your guess is too low. Guess again?"); }
else JOptionPane.showMessageDialog(null, "Correct!!");
最佳答案
if (cpucrazy < convertstring) {
JOptionPane.showInputDialog(null, "Your guess is too high. Guess again?"); }
else if (cpucrazy > convertstring) {
JOptionPane.showInputDialog(null, "Your guess is too low. Guess again?"); }
else JOptionPane.showMessageDialog(null, "Correct!!");
您需要在上述代码周围放置一个 while 循环(或某种类型的循环结构)并在以下情况下退出该结构:cpucrazy == convertstring
或留在循环结构中,而 cpucrazy != 转换字符串
循环可能类似于以下伪代码:
b = random();
do
{
input a;
if( a != b )
{
if(a < b )
{
print "Too low";
}
else
{
print "Too high";
}
}
} while( a != b );
print "You got it correct"
关于java - 再试一次 Do-While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54155720/