【问题标题】:High or low game without looping or "while" code [duplicate]没有循环或“while”代码的高或低游戏[重复]
【发布时间】:2017-05-30 16:46:11
【问题描述】:

我是一名学生,目前正在使用 java,我很难编写高低猜谜游戏。我不能使用循环或“while”代码。有什么想法吗?这是我现在所拥有的:

public class FinalProject1

{

  public static void main(String [] args)

    {

       System.out.println("Number Guessing Game 1-1000\nGuess a number");
       guess();
    }

  public static int random()

    {
        int x = (int)(1000*Math.random() + 1);
        return x;
    }

  public static void guess()

  {

      int num = random();
      int tries = 0;
      Scanner keyboard = new Scanner(System.in);
      String inputString = keyboard.nextLine();
      int input = Integer.parseInt(inputString);

      if (input > num)

      {
          System.out.println("Guess a higher number");
          inputString = keyboard.nextLine();
      }

      else if (input < num)

      {
          System.out.println("Guess a lower number");
          inputString = keyboard.nextLine();
      }

      else if (num == input)

      {
          System.out.println("You Win");

  }

}

} 

【问题讨论】:

  • 如果你不懂递归,请从头看这句话。
  • @ElliottFrisch 并单击 here 了解更多信息:)

标签: java


【解决方案1】:

一些事情..

  1. 你的 > &
  2. 您需要使用递归才能在没有任何循环的情况下产生预期的行为。

    public static void main(String [] args)
    {
    
        System.out.println("Number Guessing Game 1-1000\nGuess a number");
        int num = random();
        Scanner keyboard = new Scanner(System.in);
    
        guess(keyboard, num);
    }
    
    public static int random()
    {
        int x = (int)(1000*Math.random() + 1);
        return x;
    }
    
    public static void guess(Scanner keyboard, int goal)
    {
        String inputString = keyboard.nextLine();
        int input = Integer.parseInt(inputString);
    
        if (input < goal)
        {
            System.out.println("Guess a higher number");
            guess(keyboard, goal);
        }
    
        else if (input > goal)
        {
            System.out.println("Guess a lower number");
            guess(keyboard, goal);
        }
    
        else if (input == goal)
        {
            System.out.println("You Win");
        }
    }
    

基本上正在发生的事情是,我们正在获取他们的响应并检查它是否是 > 或 guess() 方法以重复此过程,直到他们正确为止。

一开始就很难理解递归,请继续练习

【讨论】:

  • 进行了您建议的所有更改并更改了一些内容。非常感谢您的帮助。
  • 没问题! :D 随意 +1
【解决方案2】:

首先,您的逻辑有缺陷。如果input &gt; num 则用户猜得太高,需要猜一个较低的数字。

其他人建议使用递归,这是解决您的问题的常见解决方案,并且可能是您需要做的,因为您可能刚刚了解了递归。

但是,由于我喜欢相反,我会通过启动一个新线程来做到这一点,如下所示:

public class FinalProject1 implements Runnable {
    private Scanner keyboard = new Scanner(System.in);
    private int num = random();

    public static void main(String[] args) {
        System.out.println("Number Guessing Game 1-1000\nGuess a number");
        new Thread(new FinalProject1()).start();
    }

    public static int random() {
        return (int) (1000 * Math.random() + 1);
    }

    @Override
    public void run() {
        String inputString = this.keyboard.nextLine();
        int input = Integer.parseInt(inputString);

        if (input > this.num) {
            System.out.println("Guess a lower number");
            new Thread(this).start();
        } else if (input < this.num) {
            System.out.println("Guess a higher number");
            new Thread(this).start();
        } else if (this.num == input) {
            System.out.println("You Win");
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多