我做了一个基本的数字猜谜游戏,直到我尝试在其末尾添加“再次玩”功能之前,一切都运转良好。程序运行时,输入第一个猜测后,它将仅重新开始循环,而无需经历其余的循环。另外,我不确定我的代码是否有效。对于一个简单的概念来说似乎编码太多了。这是基本猜测程序的平均长度吗?对不起,如果我的问题措辞奇怪。我是一年级的大学生,只是学习编程的基础知识。这是我的代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random randomNum = new Random();
boolean playing = true;
do {
int max = 100;
int min = 1;
int counter = 10;
int guess = 0;
int guessThis = min + randomNum.nextInt(max);
System.out.println("I'm thinking of a number between 1 and 100. You have 10 tries to guess it. What's your first guess?");
guess = input.nextInt();
counter--;
if (guess == guessThis) {
playing = false;
} else {
playing = true;
}
if (guess > max) {
System.out.println("I said the number is between 1 and 100. You think this is a GAME MUTHA FUCKA??! Guess again... :) " + counter + " guesses left.");
}
if (min > guess) {
System.out.println("Bruh. Are you stupid? " + guess + " is not between 1 and 100. Try again dummy boi. " + counter + " guesses left.");
}
if (guess > guessThis && min <= guess && guess >= max && playing == true && counter > 0) {
System.out.println("Too high. Guess again. " + counter + " guesses left.");
} else if (guess < guessThis && min <= guess && guess >= max && playing == true && counter >0) {
System.out.println("Too low. Guess again. " + counter + " guesses left.");
}
if (playing == false && counter > 0) {
System.out.println("You guessed it!");
}
if (counter <= 0) {
System.out.println("You lose! Ha! Fuck off broooooo. My number was " + guessThis);
playing = false;
}
}while (playing == true);
String answer;
if (playing == false) {
System.out.println("Wanna play again? (y/n)");
}
answer = input.next();
if (answer == "n") {
System.out.println("My game isn't fun enough for you? Wow, okay, rude. Bye then. Dh.");
input.close();
} if (answer == "y") {
playing = true;
}
}
}
答案 0 :(得分:-1)
使用
}while (playing == true);
if语句结束后
if (answer == "y") {
playing = true;
}