我有一个需要从 0 到 100 设置的代码,并且有可能将零作为正确答案。我已将我的最终整数设置为 MAX(100)/MIN(0)。但 0 永远不是正确答案。我有 4 年的编程经验和 2 年的 Java 经验,但似乎任何问题 1-100 或 1-任何其他 MAX 都可以。请帮助,或解释 0 是否不是允许的选择。
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random generator = new Random();
int MIN = 0;
int MAX = 100;
int answer;
int guess;
String another = "y";
boolean flag = false;
boolean anotherFlag = true;
while(anotherFlag){
answer = generator.nextInt(MAX) + 1;
System.out.println("I'm thinkin of a number between 0 and " + MAX );
System.out.println("Can you guess what it is? \n");
flag = false;
while(!flag) {
guess = scan.nextInt();
if(guess == answer){
System.out.println("You guessed correctly");
flag = true;
} else{
System.out.println("That was wrong, try again.");
}
}
System.out.println("Want to Play again?(y/n)");
another = scan.next();
if(another.equalsIgnoreCase("y") == true){
anotherFlag = true;
} else{
anotherFlag = false;
}
}
感谢您的帮助。
饮歌长啸
相关分类