for(j=0;j<z;j++)
{
if(q==a[j]&&w==b[j]&&e==c[j]) ////判断是否相同
{a[i]=random.nextInt(100); ////重新产生a[i]
b[i]=random.nextInt(100); ////重新产生b[i]
c[i]=random.nextInt()%4+1;} ////重新产生c[i]
}
可定制的实现,输入一个n,比如n=4,当(i+1)%4==0时,输出一个换行,这样就实现了每行有4个算式。
是否有乘除法的实现,若有乘除法,c取1234,若无乘除c取12.
c取1234时:
if (c[i]==1) s=‘+‘;
else if (c[i]==2) s=‘-‘;
else if (c[i]==3) s=‘*‘;
else s=‘/‘;
c取12时:
if (c[i]==1) s=‘+‘;
else s=‘-‘;
实现代码:
package 四则运算;
import java.util.Random;
import java.util.Scanner;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i,n,j,m,aa,r = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("要输出多少道四则运算题,然后敲回车");
aa= scanner.nextInt();
i=aa;
System.out.print("请输入n(每行显示n个式子),然后敲回车");
n= scanner.nextInt();
System.out.print("是否有乘除法(有 1 无 0),然后敲回车");
m= scanner.nextInt();
if(m==1)
{
System.out.print("除法有无余数(有 1 无 0),然后敲回车");
r= scanner.nextInt();
}
scanner.close();
int a[],b[],c[];
a=new int[i];b=new int[i];c=new int[i];
for(i=0;i<aa;i++)
{
int z,q,w,e;z=i;
char s;
Random random = new Random();
a[i]=random.nextInt(100);
b[i]=random.nextInt(100);
if(m==1)
{
if(r==1)//有余数
{
c[i]=random.nextInt()%4+1;
q=a[i];w=b[i];e=c[i];
for(j=0;j<z;j++)
{
if(q==a[j]&&w==b[j]&&e==c[j])
{a[i]=random.nextInt(100);
b[i]=random.nextInt(100);
c[i]=random.nextInt()%4+1;}
}
if (c[i]==1) s=‘+‘;
else if (c[i]==2) s=‘-‘;
else if (c[i]==3) s=‘*‘;
else s=‘/‘;
}
else //无余数
{
a[i]=random.nextInt(100);
b[i]=random.nextInt(100);
c[i]=random.nextInt()%4+1;
q=a[i];w=b[i];e=c[i];
for(j=0;j<z;j++)
{
if(q==a[j]&&w==b[j]&&e==c[j])
{a[i]=random.nextInt(100);
b[i]=random.nextInt(100);
c[i]=random.nextInt()%4+1;}
}
if (c[i]==1) s=‘+‘;
else if (c[i]==2) s=‘-‘;
else if (c[i]==3) s=‘*‘;
else {s=‘/‘;a[i]=a[i]/b[i]*b[i];}
}
}
else
{
c[i]=random.nextInt()%2+1;
q=a[i];w=b[i];e=c[i];
for(j=0;j<z;j++)
{
if(q==a[j]&&w==b[j]&&e==c[j])
{a[i]=random.nextInt(100);
b[i]=random.nextInt(100);
c[i]=random.nextInt()%2+1;}
}
if (c[i]==1) s=‘+‘;
else s=‘-‘;
}
System.out.print(a[i]);
System.out.print(s);
System.out.print(b[i]);
System.out.print("= ");
if((i+1)%n==0)
System.out.println("");
}
}
}