首页 > 代码库 > 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;
             }
         }
     }


}

                   运行结果

技术分享

 

技术分享

java的加减乘除问题