四则运算2的设计思想
问题:四则运算2
代码:
import java.util.Random;
import java.util.Scanner;
public class Yunsuan
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
System.out.print("请输入题目数量:");
int num=input.nextInt();
System.out.print("请输入数值范围(最小、最大)");
int sc1=input.nextInt();
int sc2=input.nextInt();
System.out.print("是否有乘除法(若有选择1、若无选择0):");
int c1=input.nextInt();
if(num<0)
System.out.print("输入有误,请重新输入!");
else
{
Random a=new Random();
Random b=new Random();
Random c=new Random(4);
int[] array1= new int[num];
int[] array2=new int[num];
int[] array3=new int [num];
for(int i=0;i<num;i++)
{
array1[i]=a.nextInt(sc2-sc1+1)+sc1;
array2[i]=b.nextInt(sc2-sc1+1)+sc1;
if(i>=1) //排除重复数
{
if(array1[i]==array1[i-1] && array2[i]==array2[i-1]&&array3[i] ==array3[i-1])
{
i--;
break;
}
}
if(c1==1)
for(int j=0;j<num;j++)
{array3[j]=c.nextInt(4); }
else
for(int m=0;m<num;m++)
{array3[m]=c.nextInt(2);}
switch(array3[i])
{case 0:
System.out.println(array1[i]+"+"+array2[i]+"=");
break;
case 1:
System.out.println(array1[i]+"-"+array2[i]+"=");
break;
case 2:
System.out.println(array1[i]+"x"+array2[i]+"=");
break;
case 3:
if(array2[i]!=0)
System.out.println(array1[i]+"/"+array2[i]+"=");
else
i--;
break;
}
}
}
}
}