我需要帮助在Java中创建一个循环的情况

时间:2013-02-15 21:25:36

标签: java loops numbers

修改

感谢您的帮助。以下是帮助我的答案: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();
        }




    }
}

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

1 个答案:

答案 0 :(得分: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