设计思路:
1.定义数组 char a[]={‘+’,’-’,’*’,’/’};
2.利用for循环进行30个算法的输出;
其中定义j=(int)(Math.random()*4);来随机对应相应的运算符a[j];
判断当为除法运算时分母是否为零,为零则停止本次运算;
代码:
package 算法; public class suanfa { public static void main(String[] args) { // TODO Auto-generated method stub char a[]={'+','-','*','/'}; int i,j; double b[]= new double[30]; double c[]=new double[30]; double d[]=new double[30]; for( i=0;i<30;i++) { b[i]=(int)(Math.random()*99); c[i]=(int)(Math.random()*99); j=(int)(Math.random()*4); if(c[i]==0&&a[j]=='/') continue; if(a[j]=='+')d[i]=b[i]+c[i]; if(a[j]=='-')d[i]=b[i]-c[i]; if(a[j]=='*')d[i]=b[i]*c[i]; if(a[j]=='/')d[i]=b[i]/c[i]; System.out.println(" "+b[i]+" "+a[j]+" "+c[i]+" =___;"+" 运行结果为:"+d[i] ); } } }
实验截图: