1、设计思想:把四则运算分为整数和分数四则运算,先做整数四则运算,在做除法时,有余数时用分数代替结果;做分数四则运算是,用分数代替解果。
2、源代码:
1 package 四则运算; 2 3 import java.util.Scanner; 4 5 public class TI { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner in = new Scanner(System.in); 10 System.out.println("请输入题目的数量:"); 11 int n = in.nextInt(); 12 System.out.println("请输入每行题目的数目:"); 13 int num= in.nextInt(); 14 int result[] = new int[n]; 15 int result_an[] = new int[n]; 16 String result1[] = new String[n]; 17 String result_an1[] = new String[n]; 18 //整数四则运算 19 for(int i = 0;i<n/2;i++){ 20 int x =1+ (int)(Math.random()*100); 21 int y = 1+(int)(Math.random()*100); 22 int fuhao = 1+(int)(Math.random()*4); 23 System.out.print(i+1+": "); 24 if(fuhao == 0) 25 { 26 System.out.print(x+ " + " +y+" "); 27 result_an[i] = in.nextInt(); 28 result[i] = x+y; 29 } 30 else if(fuhao == 1) 31 { 32 if(x<y) 33 { 34 System.out.print(y+ " - " +x+" "); 35 result_an[i] = in.nextInt(); 36 result[i] = y-x; 37 } 38 else 39 { 40 System.out.print(x+ " - " +y+" "); 41 result_an[i] = in.nextInt(); 42 result[i] = x-y;} 43 } 44 else if(fuhao ==2){ 45 System.out.print(x+ " * " +y+" "); 46 result_an[i] = in.nextInt(); 47 result[i] = x*y; 48 } 49 else 50 { 51 System.out.print(x+ " / " +y+" "); 52 result_an1[i] = in.next(); 53 result1[i] = huajian(x,y); 54 } 55 if((i+1)%num ==0) 56 { 57 System.out.print("\r"); 58 } 59 } 60 //分数四则运算 61 for(int i = n/2;i<n;i++) 62 { 63 int a =1+ (int)(Math.random()*10); 64 int b = 1+(int)(Math.random()*10); 65 int c = 1+(int)(Math.random()*10); 66 int d = 1+(int)(Math.random()*10); 67 int e; 68 int fuhao = 1+(int)(Math.random()*4); 69 70 if(a>b) 71 { 72 e = a; 73 a = b; 74 b = e; 75 } 76 if(c>d) 77 { 78 e = c; 79 c = d; 80 d = e; 81 } 82 String x = a+"/"+b; 83 String y = c+"/"+d; 84 System.out.print(i+1+": "); 85 if(fuhao == 0) 86 { 87 System.out.print(x+ " + " +y+" "); 88 a = a*d+b*c; 89 b = b*d; 90 result_an1[i] = in.next(); 91 result1[i] = huajian(a,b); 92 } 93 else if(fuhao == 1) 94 { 95 if(a/b<c/d) 96 { 97 System.out.print(y+ " - " +x+" "); 98 a = b*c-a*d; 99 b = b*d; 100 result_an1[i] = in.next(); 101 result1[i] = huajian(a,b); 102 } 103 else 104 System.out.print(x+ " - " +y+" "); 105 a = a*d-b*c; 106 b = b*d; 107 result_an1[i] = in.next(); 108 result1[i] = huajian(a,b); 109 } 110 else if(fuhao ==2){ 111 System.out.print(x+ " * " +y+" "); 112 a = a*c; 113 b = b*d; 114 result_an1[i] = in.next(); 115 result1[i] = huajian(a,b); 116 } 117 else{ 118 System.out.print(x+ " / " +y+" "); 119 a = a*d; 120 b = b*c; 121 result_an1[i] = in.next(); 122 result1[i] = huajian(a,b); 123 } 124 if((i+1)%num==0) 125 System.out.print("\r"); 126 } 127 System.out.println("若做完,按1可查看答案"); 128 int number = in.nextInt(); 129 in.close(); 130 if(number == 1){ 131 for(int i= 0;i<n;i++){ 132 System.out.print(i+1+":"); 133 if(result1[i]!=null){ 134 if(result1[i].equals(result_an1[i])) 135 System.out.println("正确"); 136 else 137 System.out.println("错误,正确答案为"+result1[i]); 138 } 139 else 140 if(result[i]==result_an[i]) 141 System.out.println("正确"); 142 else 143 System.out.println("错误,正确答案为"+result[i]); 144 } 145 } 146 } 147 public static String huajian(int a,int b){ 148 if(a<b) 149 { 150 for(int i = a;i>0;i--){ 151 if(a%i==0&&b%i==0){ 152 a/=i; 153 b/=i; 154 break; 155 }} 156 } 157 else{ 158 159 for(int i = b;i>0;i--){ 160 if(a%i==0&&b%i==0){ 161 a/=i; 162 b/=i; 163 break; 164 }} 165 166 } 167 168 return a+"/"+b; 169 } 170 }
3、运行结果截图:
原文:http://www.cnblogs.com/shouhutian/p/6486661.html