Java 如何创建一个循环来生成随机数,直到它与我的用户输入匹配?

Java 如何创建一个循环来生成随机数,直到它与我的用户输入匹配?,java,loops,random,numbers,Java,Loops,Random,Numbers,我对java非常陌生。我在做运动,我被卡住了。基本上我必须 请用户从1-100中随机选择一个。如果不是在这些之间,请再次询问 生成1-100之间的随机#,直到与用户输入匹配 打印匹配号码和尝试获取匹配号码的次数 到目前为止,我已经做到了: import java.util.Scanner; 导入java.util.Random; 公课粗人{ 公共静态void main(字符串[]args){ 扫描仪阅读器=新扫描仪(System.in); Random rand=新的Random(); int

我对java非常陌生。我在做运动,我被卡住了。基本上我必须

  • 请用户从1-100中随机选择一个。如果不是在这些之间,请再次询问
  • 生成1-100之间的随机#,直到与用户输入匹配
  • 打印匹配号码和尝试获取匹配号码的次数
  • 到目前为止,我已经做到了:

    import java.util.Scanner;
    导入java.util.Random;
    公课粗人{
    公共静态void main(字符串[]args){
    扫描仪阅读器=新扫描仪(System.in);
    Random rand=新的Random();
    int rantInt=rand.nextInt(101);
    while(true){
    System.out.println(“你好,朋友!选择一个介于1-100之间的数字:”;
    int pick=Integer.parseInt(reader.nextLine());
    如果(拾取>100){
    System.out.println(“哦,数字太大了!请重试!\n”);
    }否则{
    System.out.println(“让我们看看我是否也能得到相同的数字!”);
    }
    }
    }
    }
    

    我不知道接下来该怎么办。我应该添加另一个while循环吗?如果是的话,我应该怎么写?如何生成随机数,直到它与用户输入的数字匹配?

    是。在获得用户的选择之后,您将需要另一个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; 
    

    对。在获得用户的选择之后,您将需要另一个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; 
    

    这是一个简单的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");
    
    
    
          }
          }
    

    这是一个简单的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");
    
    
    
          }
          }
    

    我认为您只需使用
    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.");
        }
    }
    
    import java.util.Scanner;
    导入java.util.Random;
    公课粗人{
    公共静态void main(字符串[]args){
    扫描仪阅读器=新扫描仪(System.in);
    Random rand=新的Random();
    int rantInt=rand.nextInt(101);
    整数选择;
    while(true){
    System.out.println(“你好,朋友!选择一个介于1-100之间的数字:”;
    pick=Integer.parseInt(reader.nextLine());
    如果(拾取>100){
    System.out.println(“哦,数字太大了!请重试!\n”);
    }否则如果(选择<1){
    System.out.println(“哦,数字太小了!请重试!\n”);
    }
    否则{
    System.out.println(“让我们看看我是否也能得到相同的数字!”);
    中断;}
    }
    整数计数=0;
    while(rantInt!=拾取){
    计数++;
    rantInt=rand.nextInt(101);
    }
    System.out.println(使用“+count+”尝试找到pick+”);
    }
    }
    
    我认为您只需使用
    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.");
        }
    }
    
    import java.util.Scanner;
    导入java.util.Random;
    公课粗人{
    公共静态void main(字符串[]args){
    扫描仪阅读器=新扫描仪(System.in);
    Random rand=新的Random();
    int rantInt=rand.nextInt(101);
    整数选择;
    while(true){
    System.out.println(“你好,朋友!选择一个介于1-100之间的数字:”;
    pick=Integer.parseInt(reader.nextLine());
    如果(拾取>100){
    System.out.println(“哦,数字太大了!请重试!\n”);
    }否则如果(选择<1){
    System.out.println(“哦,数字太小了!请重试!\n”);
    }
    否则{
    System.out.println(“让我们看看我是否也能得到相同的数字!”);
    中断;}
    }
    整数计数=0;
    while(rantInt!=拾取){
    计数++;
    rantInt=rand.nextInt(101);
    }
    System.out.println(使用“+count+”尝试找到pick+”);
    }
    }
    
    注意,
    rand.nextInt(101)
    生成一个介于0和100之间的数字,而不是1和100之间的数字。哦,我明白了。那么应该如何写呢?@TracyNguyen
    int-rantInt=rand.nextInt(100)+1注意,
    rand.nextInt(101)
    生成一个介于0和100之间的数字,而不是1和100之间的数字。哦,我明白了。那么应该如何写呢?@TracyNguyen
    int-rantInt=rand.nextInt(100)+1