使用do-while循环为随机数猜测游戏生成新的随机数

时间:2019-05-16 01:32:47

标签: java

目标:创建一个随机产生1-100的数字并要求用户猜测的游戏。

问题是:我问用户是否要退出(如果不退出程序)是否继续,如果要退出,我想为他们生成一个新的随机数,然后尝试生成一个新的数字。我不确定该怎么做。

如果有人可以帮助我,那太好了, 谢谢!

import java.util.Scanner;
import java.util.Random;
public class SuperRandom
{
    public static void main(String [] args)
    {
        Scanner keyboard = new Scanner(System.in);
        Random randy = new Random();

        //#declaring variables
        int num, count = 0;
        final int random = randy.nextInt(100);
        final int randoms = randy.nextInt(100);
        String input;
        char yn;

        //#random number
        System.out.println("Num = " + random);

        //#title or header
        System.out.println("Random Number Guessing Game");
        System.out.println("===========================");

        //#asking user for input
        do
        {
            do
            {
                System.out.print("Guess the random number " + 
                    "from 1 to 100===> ");
                num = keyboard.nextInt();

                //#if the number the user entered
                //#was less than the random number
                if(num < random)
                {
                    //#display this message
                    System.out.println("Your guess is too low try again...");
                    System.out.println();
                    if (num < 1)
                    {
                        System.out.println("Invalid Input" +
                            "\nYour guess cannot be a negative" +
                            " number");
                    }
                }
                //#if the number the user entered
                //#was less than the random number
                if(num > random)
                {
                    //#display this message
                    System.out.println("Your guess is too high try again...");
                    System.out.println();
                    if (num > 100)
                    {
                        System.out.println("Invalid Input"+
                           "\nYour guess cannot be above 100");
                    }

                }

                count++;
                if (num == random) 
                {
                    System.out.println("You guessed the random number in " + 
                        count + " guesses!");
                    break;
                } 

            }
            while (num > 1 || num < 100);
            yn = 'x'; 
            System.out.print("Continue? (Y or N)==> ");
            input = keyboard.next();
            yn = input.charAt(0);

        }
        while (yn == 'Y' || yn == 'y');

    }
}

1 个答案:

答案 0 :(得分:0)

您可以在外循环的开头或结尾生成随机数。在这种情况下,您将需要从random的声明中删除final关键字,因为这会告诉编译器该值永远不会更改。


// declarations and header
int random;
//#asking user for input
        do
        {
            random = randy.nextInt(100); // here
            System.out.println("Num = " + random);
          do
            {
              // guessing loop
            }
            while (num > 1 || num < 100);
            random = randy.nextInt(100); // or here if you want to keep the line that generates the random number before the loop


            // checks for continue
         }
         while (yn == 'Y' || yn == 'y');

显然最好在外循环的开始生成它。

我也不知道您对内部循环保护的含义是什么,但是任何大于1或小于100的整数都可以满足。