0

编写一个程序,生成一个介于 1 到 100 之间的随机数,并将其保存为秘密数字。然后程序将检查用户是否可以猜出密码。用户可以继续猜数字直到找到数字,或者用户可以输入 0,这将终止程序。

每次用户进行猜测时,程序都会报告如下:

  1. 太高或太低(超过 30 折)
  2. 高或低(相差 10 到 30 分)
  3. 有点高或有点低(低于 10 分)

如果密码是 74 并且用户输入 26,程序将打印“Way too Low”。如果用户然后说 65,那么程序将打印“A Little Low

我被 if 语句卡住了,也许我的结构不正确。我不确定 。

import java.util.Scanner;
public class SecretNumber {
    public static void main(String[] args){
        int random1, answer;
        Scanner input = new Scanner(System.in);

        random1 = (int)(Math.random()*10);
        System.out.print(random1);

        System.out.println("Guess the number");
        answer = input.nextInt();

        while(answer != 0) {

            if (answer > (random1 + 30)){

                System.out.println("Way to high");
            }
            else if ( answer > ( random1 - 30)){

                System.out.println("Way to low");

            }
            else if (answer > random1 + 10 && answer < random1 + 30){

                System.out.println("High");

            }
            else if (answer > random1 - 10 && answer < random1 - 30 ){

                System.out.println("Low");

            }
            else if ( answer > random1 + 10){

                System.out.println("A little high");
            }
            else if ( answer < random1 - 10){

                System.out.println("A little low");

            }
            else if ( answer == random1){

                System.out.println("That is correct");
                System.exit(0);
            }
            else {
                System.out.println("Guess the number");
                answer = input.nextInt();
            }
        }
    }
}
4

2 回答 2

2

将您从最后一个 else 块中获得输入的行移到 else 之后(但仍在您的循环中),它将正常工作。

您要这样做的原因是因为它再次进入猜测条件之一(例如,“太低了!”),然后永远不会进入最终的 else,从而导致无限循环。

    while (answer !=0){

        // ommitted

        else if ( answer == random1){

            System.out.println("That is correct");
            System.exit(0);
        }

        System.out.println("Guess the number");
        answer = input.nextInt();
    }
于 2013-03-11T22:50:37.897 回答
0

我进行了一些更改并以某种方式格式化了我觉得更好但是您可以根据自己的喜好复制和粘贴并重新格式化它,我还删除了路径并将其更改为低于或高于但如果您可以将其添加回来你喜欢

/*
*/
package randomnumbergame;

import java.util.Scanner;

public class Randomnumbergame 
{

public static void main(String[] args) 
{
    int random1, answer;

    Scanner input = new Scanner(System.in);

    random1 = (int)(Math.random()*100);


    System.out.println("Guess the number I am thinking of, it is between 1 and a 100 or press 0 to quite");
    answer = input.nextInt();

    while(answer != 0) 
    {

        if (answer > random1)
        {

            System.out.println("Your guess is to high");
        }
        else if ( answer > random1)
        {

            System.out.println("Your guess is to low");

        }         
        else 
        {

            System.out.println("That is correct");

        }

    System.out.println("Guess the number I am thinking of, it is between 1 and a 100 or press 0 to quite");
    answer = input.nextInt();

    }
} 
}

另一方面,该程序正在为我工​​作,但无论我似乎猜测什么,它通常似乎告诉我我是正确的 =/ ,我知道 math.random 部分是正确的,因为这是我今天在课堂上使用的这也是我的导师告诉我要使用的。

于 2014-09-17T01:07:58.010 回答