这是由Java编写的300道随机四则运算小程序, 运算数范围为0~100(不包括100),运算结果保留两位小数。

程序代码:

import java.util.*;
class Yunsuan{
public static void main(String[] args){
Random rand = new Random();
for(int i = 0; i < 300; i++){
int num1 = (int)(rand.nextDouble() * 100);
int num2 = (int)(rand.nextDouble() * 100);
double num3 = 0;
char ch = 0;
switch(rand.nextInt(4)){
case 0: ch = '+'; num3 = 1.0 * num1 + num2; break;
case 1: ch = '-'; num3 = 1.0 * num1 - num2; break;
case 2: ch = '×'; num3 = 1.0 * num1 * num2; break;
case 3: ch = '÷'; while(num2 == 0){num2 = (int)(rand.nextDouble() * 100);} num3 = 1.0 * num1 / num2; break;
}
num3 *= 100;
num3 = Math.round(num3);
num3 /= 100;
if((num3 - (int)num3) == 0){
System.out.println(num1 + "" + ch + num2 + "=" + (int)num3);
}
else{
System.out.println(num1 + "" + ch + num2 + "=" + num3);
}
} }
}

300道随机四则运算小程序(java编写)-LMLPHP

04-22 10:59