13--Math类&Random相关习题

本文提供五个编程习题,涉及Math类和Random的使用:1. 模拟简单计算器;2. 创建随机验证码;3. 统计数字频率;4. 设计猜数字游戏;5. 生成不重复的随机整数。每个题目均有参考答案。
摘要由CSDN通过智能技术生成

第一题

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

     接收三个参数,一个整数,一个运算符,另一个整数。
     计算出运算结果。
     无法运算时,返回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 {
   
           
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值