【问题标题】:How do I create a loop that will generate random numbers until it matches my user input?如何创建一个循环来生成随机数,直到它与我的用户输入匹配?
【发布时间】:2020-04-28 08:45:54
【问题描述】:

我是 Java 新手。我正在做运动,我被困住了。基本上我必须

  1. 让用户从 1-100 中随机选择一个#。如果不在这两个 # 之间,请再次询问。
  2. 在 1-100 之间生成随机 #,直到它与用户输入匹配。
  3. 打印匹配数和尝试获取匹配数的次数。

到目前为止,我已经这样做了:

import java.util.Scanner;
import java.util.Random;

public class RoughNums {
   public static void main(String[]args) {
      Scanner reader = new Scanner(System.in);
      Random rand = new Random();
      int rantInt = rand.nextInt(101);

      while(true) {
         System.out.println("Hello friend! Pick a number between 1 - 100: ");
         int pick = Integer.parseInt(reader.nextLine());

         if(pick > 100) {
            System.out.println("Oops, number too big! Please try again!\n");
         } else {
            System.out.println("Let's see if I can get the same number too!");
         }

      }

   }
}

我不知道从这里去哪里。我应该添加另一个while循环吗?如果是这样,我应该怎么写?如何生成随机数,直到它与用户输入的数字匹配?

【问题讨论】:

  • 请注意,rand.nextInt(101) 生成一个介于 0 和 100 之间的数字,而不是 1 和 100。
  • 哦,我明白了。那应该怎么写呢?
  • @TracyNguyen int rantInt = rand.nextInt(100)+1;

标签: java loops random numbers


【解决方案1】:

是的。获得用户选择后,您将需要另一个 while 循环来生成随机数,直到它与用户选择匹配。基本上它可能看起来像这样:

int rantInt;
while(true) {
   // Add code to count number of tries
   rantInt = rand.nextInt(101);
   if(rantInt == pick) { break; }  
}

另一种在最小和最大(包括)数字之间生成随机数的方法是:

int range = max - min + 1; 
int rand = (int)(Math.random() * range) + min; 

【讨论】:

    【解决方案2】:

    这是一个简单的 while 循环,带有用于查找它的布尔值和用于尝试的计数器。 rand.nextInt(101) 也会得到介于 0 - 100 之间的数字,所以你必须像我在示例中所做的那样进行更改。我一开始就发起了int pick,所以如果用户输入错误的数字,我们就不会创建两次

    public class RoughNums{
       public static void main(String[]args){
          Scanner reader = new Scanner(System.in);
          Random rand = new Random();
          int pick;
          int rantInt = 0;
          int tries = 0;
          boolean found = false;
    
             while(true) {
    
               System.out.println("Hello friend! Pick a number between 1 - 100: ");
               pick = Integer.parseInt(reader.nextLine());
    
              if(pick > 100){
              System.out.println("Oops, number too big! Please try again!\n");
    
                      }
              else {
              System.out.println("Let's see if I can get the same number too!");
    
    
              break;}
    
              }      
    
             while(!found){
                 rantInt = rand.nextInt(100) + 1; // Genrating number between 1 - 100
                 tries++;
                 if(pick == rantInt){
                     found = true;
                 }
             }
    
             System.out.println("Your number is " + rantInt + " and I found it in " + tries + " tries");
    
    
    
          }
          }
    

    【讨论】:

      【解决方案3】:

      我认为您只需使用while 循环即可完成任务,只需添加一个count 变量即可显示您需要多少次尝试才能让RNG 猜出您的号码。 这是基于您发布的代码的解决方案。

      import java.util.Scanner;
      import java.util.Random;
      
      public class RoughNums{
          public static void main(String[]args){
              Scanner reader = new Scanner(System.in);
              Random rand = new Random();
              int rantInt = rand.nextInt(101);
              int pick;
      
              while(true) {
      
                  System.out.println("Hello friend! Pick a number between 1 - 100: ");
                  pick = Integer.parseInt(reader.nextLine());
      
                  if(pick > 100){
                      System.out.println("Oops, number too big! Please try again!\n");
      
                  } else if(pick < 1){
                      System.out.println("Oops, number too small! Please try again!\n");
      
                  }
                  else {
                      System.out.println("Let's see if I can get the same number too!");
      
      
                      break;}
      
              }
      
              int count = 0;
              while (rantInt != pick){
                  count++;
                  rantInt = rand.nextInt(101);
              }
      
              System.out.println( pick + " was found using " + count + " tries.");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-02
        • 2012-09-21
        • 1970-01-01
        • 2014-01-02
        • 1970-01-01
        • 2021-07-03
        • 2017-11-30
        • 1970-01-01
        相关资源
        最近更新 更多