Java Basic High:低猜测游戏帮助(循环)

时间:2014-07-26 16:49:26

标签: java

我现在停止编程一段时间了。可能大概4年左右,而我只是想搞砸它,所以我决定做一个高:低数字猜谜游戏。 (猜一个数字1-100,程序说如果你的猜测太高或太低)我完全忘记了我会怎么做:

a)一旦用户猜出正确的号码,询问他们是否想再玩一次 b)如果他们没有猜出正确的数字(太高或太低),程序会让他们再次猜测。

我知道你需要循环,但我忘记了如何去做它们

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");
        }


    }

}

2 个答案:

答案 0 :(得分:4)

您需要将所有内容包装在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循环重复其中的任何内容,直到其()内的语句为false。在我们的例子中,循环将永远存在,因为true()内,但是使用break语句,循环将在用户正确猜测时结束。

答案 1 :(得分:0)

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");
            }

       }

    }
}

以上是一些可能有用的示例代码。 我建议制作一个播放方法,然后在你的main方法中调用它。 这使得您的代码更有条理和可读性,因为现在您可以获得所需的功能而无需使用1个带有2个循环的混乱方法。

你会注意到我包含while循环而不是循环。这是因为当您不知道您需要迭代多少次时,循环是理想的。

主方法中的while检查用户是否想要另一个游戏。请注意,它假定用户想要玩至少一个游戏。我通过在进入循环之前将wantstoplay设置为true来完成此操作,但这也可以通过do-while循环完成。有关更多信息,请参阅(http://www.java-examples.com/do-while-loop

播放方法中的while会检查用户是否需要进行另一次猜测,因为他还没有得到答案。就像我们无法知道用户想要玩多少次一样,我们也不知道用户会采取多少猜测。

希望这有助于您重新开始编程!