设计思想:先随机生成四个数,生成的概率都是一样的,所以三十道题的四则运算的数量差不多。
源代码:
整数:
package 四则运算; import java.util.Random; public class Test { public static void main(String[] args) { for(int i=0;i<30;i++) { int i1=new Random().nextInt(4); int i2,i3; if(i1==0) { i2=new Random().nextInt(100)+1; i3=new Random().nextInt(100)+1; System.out.println(i2+"+"+i3+"="); } else if(i1==1) { i2=new Random().nextInt(100)+1; i3=new Random().nextInt(100)+1; System.out.println(i2+"-"+i3+"="); } else if(i1==2) { i2=new Random().nextInt(100)+1; i3=new Random().nextInt(100)+1; System.out.println(i2+"/"+i3+"="); } else if(i1==3) { i2=new Random().nextInt(100)+1; i3=new Random().nextInt(100)+1; System.out.println(i2+"*"+i3+"="); } } } }
分数:
package 四则运算; import java.util.Random; public class Test1 { public static void main(String[] args) { for(int i=0;i<30;i++) { int i1=new Random().nextInt(4); int i2,i3; int i4,i5; if(i1==0) { i2=new Random().nextInt(100)+1; i3=new Random().nextInt(100)+1; i4=new Random().nextInt(100)+1; i5=new Random().nextInt(100)+1; System.out.println(i2+"/"+i3+"+"+i4+"/"+i5+"="); } else if(i1==1) { i2=new Random().nextInt(100)+1; i3=new Random().nextInt(100)+1; i4=new Random().nextInt(100)+1; i5=new Random().nextInt(100)+1; System.out.println(i2+"/"+i3+"-"+i4+"/"+i5+"="); } else if(i1==2) { i2=new Random().nextInt(100)+1; i3=new Random().nextInt(100)+1; i4=new Random().nextInt(100)+1; i5=new Random().nextInt(100)+1; System.out.println(i2+"/"+i3+"*"+i4+"/"+i5+"="); } else if(i1==3) { i2=new Random().nextInt(100)+1; i3=new Random().nextInt(100)+1; i4=new Random().nextInt(100)+1; i5=new Random().nextInt(100)+1; System.out.println(i2+"/"+i3+"/"+i4+"/"+i5+"="); } } } }
截图: