import javax.swing.*; public class YunS { public static void main(String[] args){ int a[]=new int[30];//第一个数的分子 int b[]=new int[30];//第一个数的分母 int c[]=new int[30];//用于判断加减乘除号 int d[]=new int[30];//第二个数的分子 int e[]=new int[30];//第二个数的分母 String f[]=new String[30];//答案 for(int i=0;i<30;i++) { a[i]=(int)(Math.random()*100+1); b[i]=(int)(Math.random()*100+1);//分母不能为0 int n=MaxCommonDivisor(a[i],b[i]); c[i]=(int)(Math.random()*100); d[i]=(int)(Math.random()*100+1); e[i]=(int)(Math.random()*100+1);//分母不能为0 int m=MaxCommonDivisor(d[i],e[i]); //加法运算 if(c[i]>=0&&c[i]<25) { if(n==b[i]&&m==e[i]) {System.out.println(a[i]/b[i]+"+"+d[i]/e[i]+"=");f[i]=(a[i]/b[i])+(d[i]/e[i])+"";} else if(n==b[i]&&m!=e[i]) {System.out.println(a[i]/b[i]+"+"+d[i]/m+"/"+e[i]/m+"="); f[i]=(a[i]/b[i])*(e[i]/m)+d[i]/m+"/"+e[i]/m;} else if(n!=b[i]&&m==e[i]) {System.out.println(a[i]/n+"/"+b[i]/n+d[i]/e[i]+"="); f[i]=a[i]/n+(d[i]/e[i])*(b[i]/n)+"/"+b[i]/n;} else if(n!=b[i]&&m!=e[i]) {System.out.println(a[i]/n+"/"+b[i]/n+"+"+d[i]/m+"/"+e[i]/m+"="); int k=MaxCommonDivisor(a[i]*e[i]+b[i]*d[i],b[i]*e[i]); if(k==b[i]*e[i]) f[i]=(a[i]*e[i]+b[i]*d[i])/k+""; else if(k!=b[i]*e[i]) f[i]=(a[i]*e[i]+b[i]*d[i])/k+"/"+b[i]*e[i]/k; } } //减法运算 else if(c[i]>=25&&c[i]<50) { if(n==b[i]&&m==e[i]) {System.out.println(a[i]/b[i]+"-"+d[i]/e[i]+"=");f[i]=(a[i]/b[i])-(d[i]/e[i])+"";} else if(n==b[i]&&m!=e[i]) {System.out.println(a[i]/b[i]+"-"+d[i]/m+"/"+e[i]/m+"="); f[i]=(a[i]/b[i])*(e[i]/m)-d[i]/m+"/"+e[i]/m;} else if(n!=b[i]&&m==e[i]) {System.out.println(a[i]/n+"/"+b[i]/n+"-"+d[i]/e[i]+"="); f[i]=a[i]/n-(d[i]/e[i])*(b[i]/n)+"/"+b[i]/n;} else if(n!=b[i]&&m!=e[i]) {System.out.println(a[i]/n+"/"+b[i]/n+"-"+d[i]/m+"/"+e[i]/m+"="); int k=MaxCommonDivisor(a[i]*e[i]+b[i]*d[i],b[i]*e[i]); if(k==b[i]*e[i]) f[i]=(a[i]*e[i]-b[i]*d[i])/k+""; else if(k!=b[i]*e[i]) f[i]=(a[i]*e[i]-b[i]*d[i])/k+"/"+b[i]*e[i]/k; } } //乘法运算 else if(c[i]>=50&&c[i]<75) { if(n==b[i]&&m==e[i]) {System.out.println(a[i]/b[i]+"X"+d[i]/e[i]+"=");f[i]=(a[i]/b[i])*(d[i]/e[i])+"";} else if(n==b[i]&&m!=e[i]) {System.out.println(a[i]/b[i]+"X"+d[i]/m+"/"+e[i]/m+"="); int k=MaxCommonDivisor(a[i]/n*d[i],e[i]); if(k==e[i]) f[i]=a[i]*d[i]/n/e[i]+""; else if(k!=e[i]) f[i]=a[i]*d[i]/n/k+"/"+e[i]/k; } else if(n!=b[i]&&m==e[i]) {System.out.println(a[i]/n+"/"+b[i]/n+"X"+d[i]/e[i]+"="); int k=MaxCommonDivisor(a[i]*d[i]/m,b[i]); if(k==b[i]) f[i]=a[i]*d[i]/m/b[i]+""; else if(k!=e[i]) f[i]=a[i]*d[i]/m/k+"/"+b[i]/k;} else if(n!=b[i]&&m!=e[i]) {System.out.println(a[i]/n+"/"+b[i]/n+"X"+d[i]/m+"/"+e[i]/m+"="); int k=MaxCommonDivisor(a[i]*d[i]/m/n,b[i]*e[i]/m/n); if(k==b[i]*e[i]/m/n) f[i]=a[i]*d[i]/b[i]/e[i]+""; else if(k!=b[i]*e[i]/m/n) f[i]=a[i]*d[i]/k/m/n+"/"+b[i]*e[i]/k/m/n; } } //除法运算 else if(c[i]>=75&&c[i]<100) { if(n==b[i]&&m==e[i]) {System.out.println(a[i]/b[i]+"÷"+d[i]/e[i]+"=");f[i]=(a[i]/b[i])/(d[i]/e[i])+"";} else if(n==b[i]&&m!=e[i]) {System.out.println(a[i]/b[i]+"÷"+d[i]/m+"/"+e[i]/m+"="); int k=MaxCommonDivisor(a[i]/n*e[i],d[i]); if(k==d[i]) f[i]=a[i]*e[i]/n/d[i]+""; else if(k!=d[i]) f[i]=a[i]*e[i]/n/k+"/"+d[i]/k; } else if(n!=b[i]&&m==e[i]) {System.out.println(a[i]/n+"/"+b[i]/n+"÷"+d[i]/e[i]+"="); int k=MaxCommonDivisor(a[i]*m,b[i]*d[i]); if(k==b[i]*d[i]) f[i]=a[i]*m/b[i]/d[i]+""; else if(k!=b[i]*d[i]) f[i]=a[i]*m/k+"/"+b[i]*d[i]/k;} else if(n!=b[i]&&m!=e[i]) {System.out.println(a[i]/n+"/"+b[i]/n+"÷"+d[i]/m+"/"+e[i]/m+"="); int k=MaxCommonDivisor(a[i]*e[i],b[i]*d[i]); if(k==b[i]*d[i]) f[i]=a[i]*e[i]/b[i]/d[i]+""; else if(k!=b[i]*d[i]) f[i]=a[i]*e[i]/k+"/"+b[i]*d[i]/k; } } } System.out.println("答案:"); for(int i=0;i<30;i++) System.out.println(f[i]); } //求两个数的最大公约数 public static int MaxCommonDivisor(int m,int n){ if(m<n){int temp=m;m=n;n=temp;} while(m%n!=0){ int temp=m%n; m=n; n=temp; } return n; } }
运行结果:
编程思路:主要利用了产生随机数的方法和求最大公约数将分数约分的方法。先利用产生的随机数,根据不同的数据范围确定加减乘除,再产生4个随机数,分别作为第一个数和第二个数的分子和分母,再求分子分母的最大公约数进行约分,进行加减乘除后的分数再次进行约分。