我正在开发一个简单的游戏,用户必须猜测一个随机数。我已经设置了所有代码,除了这个事实,如果猜测太高或太低,我不知道如何允许他们重新输入数字并继续玩,直到他们得到它。它只是停止;这是代码:
import java.util.Scanner;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int random = rand.nextInt(10) + 1;
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
}
}
答案 0 :(得分:8)
为了重复任何事情你需要一个循环。
一种常见的重复方式,即在循环体中间的条件满足之前,正在构建无限循环,并添加一种方法来突破它。
在Java中创建无限循环的惯用方法是while(true)
:
while (true) {
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
break; // This ends the loop
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
}
此循环将继续其迭代,直到代码路径到达break
语句。
答案 1 :(得分:3)
您需要使用loop。代码应该像这样工作:
import java.util.Scanner;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int random = rand.nextInt(10) + 1;
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
boolean found = false;
while (!found) {
if (number == random) {
System.out.println("Good!");
found = true;
} else if (number > random) {
System.out.println("Too Big, try again:");
number = input.nextInt();
} else if (number < random) {
System.out.println("Too Small, try again:");
number = input.nextInt();
}
}
}
}
答案 2 :(得分:1)
您可以使用do...while
。
Random rand = new Random();
int random = rand.nextInt(10) + 1;
do {
Scanner input = new Scanner(System.in);
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
} while ( number != random );
答案 3 :(得分:1)
将if语句包含在do-while循环中,当用户没有猜到数字时,它会循环播放:
int number;
do {
System.out.print("Pick a number 1-10: ");
number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
} while (number != random);
答案 4 :(得分:1)
有几种技术可以循环您的请求,其中包括:
while (<condition>) { <do something> }
do { <something> } while (<condition>);
for (<init statement>, <condition>, <update statement>) { <do something> }
要显示,您可以通过使用递归避免使用上述显式循环结构之一:
mport java.util.Scanner;
import java.util.Random;
public class Test {
public static void ask(int random) {
Scanner input = new Scanner(System.in);
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
ask(random);
} else if (number < random) {
System.out.println("Too Small");
ask(random);
}
}
public static void main(String[] args) {
Random rand = new Random();
int random = rand.nextInt(10) + 1;
ask(random);
}
}
这里ask()
方法一直在调用自己,直到达到结束条件(用户猜对了)。
根据Java虚拟机的聪明程度,这可能会对调用堆栈造成压力。
答案 5 :(得分:0)
为了有条件地重复代码,请使用循环。
// this code will execute only once
System.out.print("Pick a number 1-10: ");
// initialize number to a value that would not be used and not equal random
int number = -1;
// the code inside the curly braces will repeat until number == random
while (number != random) {
// get next number
number = input.nextInt();
// handle case one
if(number > random) System.out.println("Too Big");
// handle case two
if(number < random) System.out.println("Too Small");
}
// once number == random, the condition is false so we break out of the loop
System.out.println("Good!");
答案 6 :(得分:-1)
您正在寻找的是编程语言中的结构,允许您一次又一次地执行特定的操作。
这是使用循环完成的。 例如,检查docs for the while loop。这就是你需要的。