【问题标题】:While loop until you randomly roll selected number, where to put randomWhile 循环,直到您随机滚动选定的数字,随机放置的位置
【发布时间】:2019-09-25 01:36:44
【问题描述】:

在练习 while 循环时,我尝试在您放置随机数的位置编写代码,然后您猜测滚动它需要多少次尝试,但我不能在 while 内声明变量“机会”,但如果我把它放在它之前它只是保持滚动 1 个数字。

    Random rng = new Random();
    Scanner input = new Scanner(System.in);
    System.out.println("Select a number you want to roll");
    int choice = input.nextInt();
    System.out.println("You feelin' lucky?\nHow many tries until you get " + choice);
    int tries = input.nextInt();
    int count = 0;
    int chance = rng.nextInt((100)+1);
        while (choice != chance) {
            System.out.println(chance);
            count++;

    }
    System.out.println("You won! It only took " + count + " tries.");
}

如何声明 int 机会使其进入 while 循环?

【问题讨论】:

  • 您需要在循环内重新分配机会...即chance = rng.nextInt(101);
  • 你知道它可能永远运行吗?

标签: java loops random while-loop


【解决方案1】:

您可以在 while 循环中将机会重新分配给新值:

int count = 0;
int chance = rng.nextInt((100)+1);
while (choice != chance) {
    System.out.println(chance);
    chance = rng.nextInt((100)+1);
    count++;
}

【讨论】:

    【解决方案2】:

    如果我理解您的问题,我认为您应该使用do-while 循环。它将进入循环至少一次。

    Random rng = new Random();
    Scanner input = new Scanner(System.in);
    System.out.println("Select a number you want to roll");
    int choice = input.nextInt();
    System.out.println("You feelin' lucky?\nHow many tries until you get " + choice);
    int tries = input.nextInt();
    int count = 0;
    
    do {
       int chance = rng.nextInt((100)+1);
       System.out.println(chance);
       count++;
    } while (choice != chance)
    
    System.out.println("You won! It only took " + count + " tries.");
    

    【讨论】:

      【解决方案3】:

      不要再次声明变量chance。只需将其重新分配给一个新值即可。

      chance = rng.nextInt((100)+1);
      

      代码中的问题:

      1. 循环从不使用tries
      2. 代码无法确定尝试次数何时不足以猜测 用户输入的号码

      以下内容针对他们:

      Random rng = new Random();
      Scanner input = new Scanner(System.in);
      System.out.println("Select a number you want to roll");
      int choice = input.nextInt();
      System.out.println("You feelin' lucky?\nHow many tries until you get " + choice);
      int tries = input.nextInt();
      int count = 1;
      int chance = rng.nextInt((100) + 1);
      
      while (tries > 0) {
          System.out.println(chance);
          if (choice == chance)
              break;
          chance = rng.nextInt((100) + 1);
          count++;
          tries--;
      }
      
      if (choice == chance) {
          System.out.println("You won! It only took " + count + " tries.");
      } else {
          System.out.println("You lost");
      }
      

      逻辑:

      1. 使用tries 确定循环需要运行多少次。 每次运行后递减。
      2. 如果选择和机会相等,则控制跳出循环。
      3. 最后一个 if 条件是确定用户是否能够 在 尝试次数内猜测

      【讨论】:

        猜你喜欢
        • 2016-12-29
        • 1970-01-01
        • 2020-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-15
        • 1970-01-01
        相关资源
        最近更新 更多