13--Math类&Random相关习题

第一题

  • 模拟简单计算器,可以运算+,—,*,/,%。

     接收三个参数,一个整数,一个运算符,另一个整数。
     计算出运算结果。
     无法运算时,返回null。
    
  • 代码实现,效果如图所示:
    在这里插入图片描述

  • 参考答案:

public class Test1 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int a = scanner.nextInt();
       String next = scanner.next();
       int b = scanner.nextInt();
       String count = count(a, next, b);
       System.out.println(a +next +b +"="+count);
  }

   public static String count(int a, String op , int b ){
       int r=0;
       if ("+".equals(op)){
           r = a+b;
      }else  if ("-".equals(op)){
           r = a-b;
      }else  if ("*".equals(op)){
           r = a*b;
      }else  if ("/".equals(op)){
           r = a/b;
      }else  if ("%".equals(op)){
           r = a%b;
      }else {
           return null;
      }

       return r+"";
  }
}

第二题

  • 随机验证码。

     随机生成十组六位字符组成的验证码。
     验证码由大小写字母、数字字符组成。
    
  • 代码实现,效果如图所示:
    在这里插入图片描述

  • 开发提示:

     使用字符数组保存原始字符,利用Random类生成随机索引。
    
  • 参考答案:

public class Test2 {
   public static void main(String[] args) {
       for (int i = 0; i < 10; i++) {
           String s = verifyCode();
           System.out.println("随机验证码:" + s);
      }
  }
   public static String verifyCode() {
       char[] arr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

       Random random = new Random();
       String code  = "";
       for (int i = 0; i < 6; i++) {
           int index  = random.nextInt(arr.length);
          code  += arr[index];

      }
       return code;
  }
}

第三题

  • 统计数字出现次数。

     定义getNumList方法,随机生成100个数字,数字范围从1到10。
     定义printCount方法,统计每个数字出现的次数并打印到控制台。
    
  • 代码实现,部分效果如图所示:
    在这里插入图片描述

  • 参考答案:

public class Test3 {
   public static void main(String[] args) {
       ArrayList<Integer> numList = getNumList();
       // 统计字符数组中字母出现次数
       printCount(numList);
  }
   public static void printCount(ArrayList<Integer> list) {
       int[] count = new int[10];
       // 对应保存数字出现的次数
       for (int i = 0; i < list.size(); i++) {
           int c = list.get(i);
           count[c-1]++;
      }
       // 打印数字和次数
       for (int i = 0 ; i < count.length; i++) {
               System.out.println("数字:"+(i+1) + "--" + count[i]+"次");
      }
  }
   public static ArrayList<Integer> getNumList() {
       ArrayList<Integer> list = new ArrayList<>();
       Random r = new Random();
       for (int i = 0; i < 100; i++) {
           int x = r.nextInt(10) + 1;
           list.add(x);
      }
       return list;
  }
}

第四题

  • 猜数字游戏
  • 参考答案
 import java.util.Random;
       import java.util.Scanner;public class RandomDemo {
           public static void main(String[] args) {
​
               Random ran = new Random();
               int really = ran.nextInt(100)+1;
               while (true){
                   System.out.println("请输入要猜的数字:");
                   Scanner scan = new Scanner(System.in);
                   int num = scan.nextInt();
                   if (num>really){
                       System.out.println("大了");
                  }else if (num<really){
                       System.out.println("小了");
                  }else {
                       System.out.println("对了");
                       break;
                  }
              }}
      }

第五题

  • 生成6个1~33之间的随机整数,添加到集合,并遍历
public class Test01ArrayList {
   public static void main(String[] args) {
// 创建Random 对象
       Random random = new Random();
// 创建ArrayList 对象
       ArrayList<Integer> list = new ArrayList<>();
// 添加随机数到集合
       for (int i = 0; i < 6; i++) {
           int r = random.nextInt(33) + 1;
           list.add(r);
      }
// 遍历集合输出
       for (int i = 0; i < list.size(); i++) {
           System.out.println(list.get(i));
      }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_44787898/article/details/106801549