英文:
JAVA - End a loop after 10 repeats
问题
Scanner guess = new Scanner(System.in);
Random hundred = new Random();
System.out.println("欢迎来到神奇数字游戏!");
System.out.println("我正在想一个介于0-100之间的数字。您有10轮机会来猜测正确的数字。");
System.out.println("祝您好运!");
int number = hundred.nextInt(101);
int attempts = 0;
while (attempts < 10) {
System.out.println("您猜测的数字是多少?");
int guess1 = guess.nextInt();
if (guess1 > number) {
System.out.println("您猜测的数字比我想的要大!");
} else if (guess1 < number) {
System.out.println("您猜测的数字比我想的要小!");
} else if (guess1 == number) {
System.out.println("恭喜您!您猜对了!");
break;
}
attempts++;
}
if (attempts == 10) {
System.out.println("很遗憾,您没有在10次内猜对。正确的数字是:" + number);
}
System.out.println("再次开始游戏?(是/否)");
String playAgain = guess.next();
if (playAgain.equalsIgnoreCase("是")) {
// 重新开始游戏的代码
}
guess.close();
请注意,代码中的注释(//)表示了我所添加的说明。如果您需要重新开始游戏,您可以在注释部分添加适当的代码。
英文:
i'm writing a game where the user needs to guess a number between 0-100. the user has 10 tries to guess the right number and after every input he needs to get back a comment.
so i wrote the code and everything works pretty good but i have a problem with breaking the loops after 10 wrong guesses and starting the game again once the game ends.
anything would help. thank you in advace!
Scanner guess = new Scanner (System.in);
Random hundred = new Random ();
System.out.println ("Welcome to Magic Number!");
System.out.println ("I'm thinking about a number between 0-100. You have 10 rounds to guess the right number.");
System.out.println ("Good Luck!");
int number = hundred.nextInt(101);
while (true) {
System.out.println ("What is the number i'm thinking about?");
int guess1 = guess.nextInt();
if (guess1 > number) {
System.out.println ("The number you gueesed is higher than the number i'm thinking about!");
} else if (guess1 < number) {
System.out.println ("This number you guessed is lower than the number i'm thinking about!");
} else if (guess1 == number) {
System.out.println ("Congratulations! You've read my mind!");
break;
}
}
答案1
得分: 1
你需要在 while 循环内部添加 numberOfTimes 变量。看一下下面的代码:
int numberOfTimes = 10;
while (numberOfTimes > 0) {
System.out.println("我在想一个什么数字呢?");
int guess1 = guess.nextInt();
if (guess1 > number) {
System.out.println("你猜的数字比我想的要大!");
} else if (guess1 < number) {
System.out.println("你猜的数字比我想的要小!");
} else if (guess1 == number) {
System.out.println("恭喜!你猜中了我的心思!");
break;
}
numberOfTimes--;
}
英文:
You have to add numberOfTimes inside your while loop
look at the code below:
int numberOfTimes = 10;
while (numberOfTimes > 0) {
System.out.println ("What is the number i'm thinking about?");
int guess1 = guess.nextInt();
if (guess1 > number) {
System.out.println ("The number you gueesed is higher than the number i'm thinking about!");
} else if (guess1 < number) {
System.out.println ("This number you guessed is lower than the number i'm thinking about!");
} else if (guess1 == number) {
System.out.println ("Congratulations! You've read my mind!");
break;
}
numberOfTimes--;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论