码迷,mamicode.com
首页 > 编程语言 > 详细

java的加减乘除问题

时间:2017-03-07 22:50:20      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:rgs   ack   java   void   gif   ble   乘法   com   new   

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的加减乘除问题

标签:rgs   ack   java   void   gif   ble   乘法   com   new   

原文地址:http://www.cnblogs.com/hudaochen/p/6517057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!