Java Basic High:Low 猜谜游戏帮助(循环)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24973305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Basic High:Low Guessing Game Help (Loops)
提问by drew bry
I stopped programming for a while now. Probably around 4 years, and I was just looking to mess around with it, so I decided to make a high:low number guessing game. (guess a number 1-100, program says if your guess is too high or too low) and I completely forgot how I would go about:
我停止了一段时间的编程。大概 4 年左右,我只是想把它弄乱,所以我决定做一个高:低数字猜谜游戏。(猜一个数字 1-100,程序会说你的猜测是太高还是太低)我完全忘记了我会怎么做:
a) Once the user guesses the correct number, asking if they want to play again b) If they don't guess the correct number (too high or too low), the program lets them guess again.
a) 一旦用户猜对了数字,询问他们是否想再玩一次 b) 如果他们没有猜到正确的数字(太高或太低),程序会让他们再猜一次。
I understand that you would need loops, but I just forgot about how I would go about them
我知道您需要循环,但我只是忘记了我将如何处理它们
package highlow;
import java.util.Random;
import java.util.Scanner;
public class guessing {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Random rand = new Random();
int tries;
int correctNum = rand.nextInt(100);
System.out.println("enter a number 1-100");
int guess1 = input.nextInt();
if(guess1 < correctNum){
System.out.println("number is too low!");
}
else if(guess1 > correctNum){
System.out.println("Number is too high!");
}
else if(guess1 == correctNum){
System.out.println("correct!");
}
else{
System.out.println("not a valid option");
}
}
}
回答by Zach
You need to wrap everything in a while
loop so that it keeps repeating until the user guesses correctly:
您需要将所有内容包装在一个while
循环中,以便它不断重复,直到用户正确猜测:
// Make the scanner, get the random number etc... Put all the setup and
// stuff you don't want to be repeated here
while (true) {
System.out.println("enter a number 0-99"); // Changed from 1-100 because rand.nextInt(100)
// returns a number between 0 and 99
// You can do correctNum += 1 to make it between 1 and 100
// But put this in before the while loop starts
int guess1 = input.nextInt();
if(guess1 < correctNum){
System.out.println("number is too low!");
}
else if(guess1 > correctNum){
System.out.println("Number is too high!");
}
else if(guess1 == correctNum){
System.out.println("correct!");
break; // <---- Add this, this will make the loop stop when the
//player gets the answer correct and therefore the program will end
}
else{
System.out.println("not a valid option");
}
}
While loops repeat whatever is inside them until the statement inside their ()
is false. In our case the loop will go forever because true
is inside the ()
but with the break
statement, the loop will end when the user guesses correctly.
While 循环重复它们内部的任何内容,直到它们内部的语句()
为假。在我们的情况下,循环还将长期持续下去,因为true
是内部的()
,但用break
的语句,当用户猜测正确的循环将结束。
回答by Kabir Peshawaria
package highlow;
import java.util.*;
public class guessing
{
public static void main (String [] args)
{
boolean wantstoplay = true;
while(wantstoplay)
{
play();
System.out.println("Would you like to play again?");
Scanner kb = new Scanner (System.In);
if ((kb.nextLine().equals("yes") || (kb.nextLine().equals("Yes"))
wantstoplay = true;
else
wantstoplay = false;
}
}
public void play()
{
boolean playing = true;
int correctNum = (int) ((Math.Random() *100) + 1);
//selects random double from [1,101) and then rounds down
int tries = 0;
while (playing)
{
Scanner input = new Scanner(System.in);
System.out.println("enter a number 1-100");
int guess = input.nextInt();
if(guess < correctNum){
System.out.println("number is too low!");
tries++;
}
else if(guess > correctNum){
System.out.println("Number is too high!");
tries++;
}
else if(guess == correctNum){
System.out.println("correct!");
if (tries > 1)
System.out.println("Congrats, you guessed the right number. It only took you " + tries + " attempts!");
else
System.out.println("You guessed it first try! good job");
}
else{
System.out.println("not a valid option");
}
}
}
}
Above is some sample code that might be helpful. I suggest making a play method, and then calling it in your main method. This makes your code more organized and readable, because now you'll get the functionalities you desired without having 1 messy method with 2 loops in it.
以上是一些可能有用的示例代码。我建议创建一个 play 方法,然后在你的 main 方法中调用它。这使您的代码更有条理和可读性,因为现在您将获得所需的功能,而无需 1 个带有 2 个循环的杂乱方法。
You'll notice I included while loops rather than for loops. This is because while loops are ideal when you don't know how many times you're going to need to iterate.
您会注意到我包含了 while 循环而不是 for 循环。这是因为当您不知道需要迭代多少次时,while 循环是理想的。
The while in the main method checks to see whether the user would like another game. Notice that it assumes that the user wants to play at least one game. I did this by setting wantstoplay as true before we entered the loop, but this also could've been done with a do-while loop. For more, see (http://www.java-examples.com/do-while-loop)
main 方法中的 while 会检查用户是否想要另一个游戏。请注意,它假设用户想要玩至少一款游戏。我通过在进入循环之前将 Wantstoplay 设置为 true 来做到这一点,但这也可以通过 do-while 循环来完成。有关更多信息,请参阅(http://www.java-examples.com/do-while-loop)
The while in the play method checks to see whether the user needs to make another guess because he hasn't gotten the answer yet. Just like we can't know how many times the user wants to play before hand, we can't know how many guesses the user will take either.
play 方法中的 while 会检查用户是否需要再次猜测,因为他还没有得到答案。就像我们无法预先知道用户想要玩多少次一样,我们也无法知道用户会猜多少次。
Hope this helps you get back into programming!
希望这可以帮助您重新开始编程!