编程题 随机自动生成300道四则运算题
package math;
import java.util.Random;
public class Work2 {
public static void main(String[] args){
char f[] = {'+','-','*','/'};
int max =100;
for(int i=0;i<300;i++){
Random random= new Random();
int a = random.nextInt(max+1);
int b = random.nextInt(max+1);
int c = random.nextInt(4);
int result = -1;
switch (c){
case 0:
result = a+b;
break;
case 1:
result = a-b;
break;
case 2:
result = a*b;
break;
case 3:
while (true){
if (a!=0&&b!=0&&a%b==0){
result = a/b;
break;
}else{
a = random.nextInt(max+1);
b = random.nextInt(max+1);
}
}
break;
default:
break;
}
System.out.println(""+a+f[c]+b+"=");
}
}
}
下面是运行结果:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步