随机生成300道四则运算
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机生成300道四则运算相关的知识,希望对你有一定的参考价值。
package demo;
import java.util.Random;
public class demo1 {
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 + "=" +result );
}
}
}
import java.util.Random;
public class demo1 {
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 + "=" +result );
}
}
}
以上是关于随机生成300道四则运算的主要内容,如果未能解决你的问题,请参考以下文章