二柱子计算题
非分数四则运算
public class compute4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //四则运算 for(int i=0;i<30;i++) { int operate; int x,y; operate=1+(int)(Math.random()*4); x=1+(int)(Math.random()*10); y=1+(int)(Math.random()*10); if(operate==1) { System.out.println(x+"*"+y+"="+(x*y)); } if(operate==2) { if(x%y!=0) { if(x%y==x) { System.out.println(x+"/"+y+"="+x+"/"+y); } else { System.out.println(x+"/"+y+"="+(x/y)+"又"+(x%y)+"/"+y); } } else { System.out.println(x+"/"+y+"="+(x/y)); } } if(operate==3) { System.out.println(x+"+"+y+"="+(x+y)); } if(operate==4) { System.out.println(x+"-"+y+"="+(x-y)); } } } }
运行结果截图
分数四则运算
public class computef { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //分数运算 for(int i=0;i<30;i++) { int a,b,c,d; a=1+(int)(Math.random()*10); b=1+(int)(Math.random()*10); c=1+(int)(Math.random()*10); d=1+(int)(Math.random()*10); int operate; operate=1+(int)(Math.random()*4); if(operate==1) { int fz,fm; fm=b*d; fz=a*d+c*b; System.out.println(simplify(a,b)+"+"+simplify(c,d)+"="+simplify(fz,fm)); } if(operate==2) { int fz,fm; fm=b*d; fz=a*d-c*b; System.out.println(simplify(a,b)+"-"+simplify(c,d)+"="+simplify(fz,fm)); } if(operate==3) { int fz,fm; fm=b*d; fz=a*c; System.out.println(simplify(a,b)+"*"+simplify(c,d)+"="+simplify(fz,fm)); } if(operate==4) { int fz,fm; fz=a*d; fm=b*c; System.out.println(simplify(a,b)+" / "+simplify(c,d)+"="+simplify(fz,fm)); } } } public static String simplify(int a,int b) { int max=0; for(int i=0;i<100;i++) { int n=i+1; if(a%n==0&&b%n==0) max=n; } return (String)(a/max+"/"+b/max); } }
运行结果截图