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;

}

}

}

}

运行结果

41bb6833c094e357fbf467e25655dcd9.png

68f724bad5a33d9a9f6bd2ca7369ecb7.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值