0

编辑

谢谢你们的帮助。这是帮助我的答案:https ://stackoverflow.com/a/14903642

这是完整的工作代码: http: //pastebin.com/1kbipuAZ

结束编辑

我应该制作一个简单的 cmd 游戏,要求用户输入一定范围内的数字。然后玩家 2 应该尝试猜测这个数字是多少。程序不断告诉“高”或“低”,直到玩家猜出数字。我想我应该使用一些 if/else 语句,也许还有 while 循环,但我不知道以什么方式。我要么得到一个无限循环,要么它在我想要它之前停止。这是我所拥有的:

    import java.io.*;

class game
{
    public static void main (String[]args) throws IOException
    {
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader stdin = new BufferedReader(inStream);

        String inData;

        System.out.println("Welcome to The Game. Player One, please enter an                                integer higher than 0, and less than 1000...\n");

        inData = stdin.readLine();

        int number = Integer.parseInt(inData);

        if(number >= 1000 || number <= 0)
        {
            System.out.println("Sorry, that number is out of the acceptable     range on numbers...\n");
        }
        else
        {
            System.out.println();
        }

        String f1;

        System.out.println("Player Two, Please enter a guess as to what Player     One's number is...\n");

        f1 = stdin.readLine();

        int guess = Integer.parseInt(f1); 


        while(guess < number)
        {
            if(guess != number)
            {
                System.out.println("Higher. Please Guess Again...\n");
            }
            else
            {
                System.out.println();
            }
            break;


        }
        while(guess > number)
        {
            if(guess != number)
            {
                System.out.println("Lower. Please Guess Again...\n");
            }
            else
            {
                System.out.println();
        }
            break;

        }
        if(number==guess)
        {
            System.out.println("Congratulations. Thank you for     playing...\n");
        }
        else
        {
            System.out.println();
        }




    }
}

我尝试了循环和语句等的不同组合,但我不能走得太远。

4

1 回答 1

1

你的代码几乎是正确的......

当第二个玩家猜出正确的数字时,您需要跳出循环。所以,

while(guess != number)

在循环中,您需要进行比较和打印。此外,您每次都需要在循环中获取输入。

代码如下:

    f1 = stdin.readLine();
    int guess = Integer.parseInt(f1); 

    while(guess != number)
    {
        if(guess < number)
        {
            System.out.println("Higher. Please Guess Again...\n");
        }
        else if(guess > number)
        {
            System.out.println("Lower. Please Guess Again...\n");
        }

        f1 = stdin.readLine();
        guess = Integer.parseInt(f1); 
    }

*已编辑* *

完整的工作程序如下:

 import java.io.*;

class game
{
    public static void main (String[]args) throws IOException
    {
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader stdin = new BufferedReader(inStream);

        String inData;

        System.out.println("Welcome to The Game. Player One, please enter an                                integer higher than 0, and less than 1000...\n");

        inData = stdin.readLine();

        int number = Integer.parseInt(inData);

        if(number >= 1000 || number <= 0)
        {
            System.out.println("Sorry, that number is out of the acceptable     range on numbers...\n");
        }
        else
        {
            System.out.println();
        }

        String f1;

        System.out.println("Player Two, Please enter a guess as to what Player     One's number is...\n");

        f1 = stdin.readLine();
        int guess = Integer.parseInt(f1); 

        while(guess != number)
        {
            if(guess < number)
            {
                System.out.println("Higher. Please Guess Again...\n");
            }
            else if(guess > number)
            {
                System.out.println("Lower. Please Guess Again...\n");
            }

            f1 = stdin.readLine();
            guess = Integer.parseInt(f1); 
        }



    }
}

这是输出:

欢迎来到游戏。玩家一,请输入一个大于0小于1000的整数...

10

玩家二,请输入猜测玩家一的号码是什么...

2 更高。请再猜一猜...

5 更高。请再猜一猜...

7 更高。请再猜一猜...

12 降低。请再猜一猜...

11 降低。请再猜一猜...

9 更高。请再猜一猜...

10

于 2013-02-15T21:31:54.397 回答