由计算机产生0-9之间的随机整数,用户输入猜测的数据,统计用户猜测的次数。产生随机数可以使用Math.rangom()静态方法,该方法返回正的double类型浮点数位于(0.0 1.0】间。

 package testrandom;

import java.util.Random;

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
           Random ra = new Random();
        Scanner sc = new Scanner(System.in);
        int temp = ra.nextInt(10);
        System.out.println(temp);
        String s = "";
        int count = 0;
        while(true){
               System.out.println("请通过键盘输入您猜的数字:");
               s = sc.next();
               if(s.matches("
//d*"))
               {
                      int in = Integer.parseInt(s);
                      if(in == temp)
                        {
                          System.out.println("您猜对了!");
                          count +=1;
                          break;
                        }
                       else
                        {
                          System.out.println("啊呀呀,还是不对,再来一次?!");
                          count++;
                         }
                }
                   else
                    {
                     System.out.println("请输入数字格式!");
                     count++;
                     continue;
                    }
  }
        if(count<=3)
        {
          System.out.println("您只猜了 " + count + " 次");
         }
        else{
               System.out.println("多多努力,您总计猜了 " + count + " 次");
             }
}
}

 

你可能感兴趣的:(由计算机产生0-9之间的随机整数,用户输入猜测的数据,统计用户猜测的次数。产生随机数可以使用Math.rangom()静态方法,该方法返回正的double类型浮点数位于(0.0 1.0】间。)