我有一个代码需要设置为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;
}
}
谢谢您的帮助.
最佳答案
原文链接:https://www.f2er.com/java/532829.html