【问题标题】:Creating a java program guessing game创建一个java程序猜谜游戏
【发布时间】:2014-09-17 09:20:11
【问题描述】:

编写一个程序,生成一个介于 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();
            }
        }
    }
}

【问题讨论】:

  • 你有什么问题?怎么没有达到你的预期?
  • 请注意random1 = (int)(Math.random()*10); 会给你0 &lt;= random1 &lt; 10
  • 好吧,它卡在第一个 if 语句上,要么在一个永无止境的循环中说高或低

标签: java


【解决方案1】:

将您从最后一个 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();
    }

【讨论】:

  • 我做了它停止了无限循环,但它仍然无法正常工作
【解决方案2】:

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

/*
*/
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 部分是正确的,因为那是我使用的今天上课,这也是我的老师告诉我要使用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多