zoukankan      html  css  js  c++  java
  • java的加减乘除问题

    package a;

    import java.text.DecimalFormat;


    public class bb {
      public static void main(String[] args) {                                         //实例化方法
             operation();
         }

         public static void operation() {
           
             for (int i = 1;  i<= 300; i++) {                                          //三百个
                 int x =  (int) (Math.random() * 4);                                   //四个方法
                 int a =  (int) (Math.random() * 100);                                 //a小于100
                 int b =  (int) (Math.random() * 100);                                 //b小于100
                 switch (x) {
                 case 0:                                                               //加法
                     System.out.println(" ");
                     System.out.println(  i + ".  "+a + "+" + b + "=" + (a + b));
                     break;
                 case 1:                                                               //减法
                  System.out.println(" ");
                     System.out.println( i + ".  "+a + "-" + b + "=" + (a - b));
                     break;
                 case 2:                                                               //乘法
                  System.out.println(" ");
                     System.out.println(  i + ".  "+a + "*" + b + "=" + (a * b));
                     break;
                 case 3:
                     if (b == 0) {                                                      //如果b=0从算
                         i--;
                         break;
                     } else {                                                           //如果是整数输出
                         if (a % b == 0) {
                          System.out.println(" ");
                            System.out.println( i + ".  "+a + "/" + b + "=" + (a / b));
                         } else {                                                       //如果是余数保留两位小数输出
                          System.out.println(" ");
                             double num = (double) a / b;
                             String ss = new DecimalFormat("0.00").format(num);
                             System.out.println( i + ".  "+a + "/" + b + "=" + ss);
                         }
                     }
                     break;
                 default:                                                               //全部结束
                     break;
                 }
             }
         }


    }

                       运行结果

  • 相关阅读:
    第87天:HTML5中新选择器querySelector的使用
    第86天:HTML5应用程序标签和智能表单
    第85天:HTML5语义化标签
    第84天:jQuery动态创建表格
    第83天:jQuery中操作form表单
    第82天:jQuery中prop()和attr()的区别
    第81天:jQuery 插件使用方法
    第80天:jQuery插件使用
    第79天:jQuery事件总结(二)
    对事务的特性ACID的理解
  • 原文地址:https://www.cnblogs.com/hudaochen/p/6517057.html
Copyright © 2011-2022 走看看