A simple addition, subtraction, multiplication, division, automatic generation of small program upgrade version (JAVA), addition, subtraction, multiplication, division, java
1 import java. util. optional; 2 public class Suan {3 public static void main (String [] args) {4 int [] B; // set the array to store the random numbers of 5 B = new int [4]; 6 bytes in = new bytes (System. in); 7 System. out. println ("select the question type you want to exercise:"); 8 System. out. println ("1. Score addition/subtraction multiplier practice! "); 9 System. out. println (" 2. integer addition/subtraction multiplier exercise! "); 10 int xuanze = in. nextInt (); 11 System. out. println ("the number of arithmetic operations you want to output is:"); 12 int num = in. nextInt (); 13 String [] a = {"+", "-", "*", "/"}; // an Integer Operation stores the 14 String res of the symbol array, res1 = null, sum1 = null; // res indicates the answer you entered. res1 is the correct answer in the fractional operation and Integer Operation Division. 15 int [] c; // The algebra of the symbol 16 int j = 0, sum2 = 0; // sum2 is the user input answer for the subtraction in the Integer Operation 17 int rshu = 0; // The user calculates the correct number 18 c = new int [4]; 19 if (1 = xuanze) // select the type of formula 20 {21 for (int I = 0; I <4; I ++) to be output. // generate a symbolic algebra 22 {23 c [I] = (Int) (Math. random () * 14/3); 24} 25 for (j = 0; j <num; j ++) // if the number of num is insufficient, the formula 26 {27 for (int I = 0; I <4; I ++) is returned) // generate a random number from 1 to 10 28 {29 B [I] = (int) (Math. random () * 10); 30} 31 32 if (B [0]> B [1] & B [2]> B [3] & B [0]! = 0 & B [1]! = 0 & B [2]! = 0 & B [3]! = 0) // prerequisite for the true score 33 {34 System. out. println ("(" + B [1] + "/" + B [0] + ") "+ a [c [0] +" ("+ B [3] +"/"+ B [2] +") "); 35 System. out. print ("your answer is:"); 36 res = in. next (); 37 switch (c [0]) {// call the member method to calculate 38 case 0: res1 = fracAdd (B [1], B [0], B [3], B [2]); break; 39 case 1: res1 = fracSub (B [1], B [0], B [3], B [2]); break; 40 case 2: res1 = fracMul (B [1], B [0], B [3], B [2]); break; 41 case 3: res1 = fractDiv (B [1], B [0], B [3], B [2]); break; 42} 43 if (! Res. equals (res1) // compare the answer 44 {45 System. out. println ("your answer is incorrect! Correct answer: "+ res1); 46} 47 else 48 {49 rshu ++; // the correct number calculated by the user 50} 51} 52 else 53 j --; 54} 55} 56 if (2 = xuanze) // select integer formula 57 {58 59 for (int r = 0; r <num; r ++) // if the number of num is not enough, the formula 60 {61 int sum =-1; // the real result is 62 for (int I = 0; I <4; I ++) 63 {64 B [I] = (int) (Math. random () * 100); 65} 66 for (int I = 0; I <4; I ++) 67 {68 c [I] = (int) (Math. random () * 14/3); 69} 70 switch (c [0]) // identify the symbol 71 {72 case 0: 73 case 1: sum = B [0] + B [1 ]; Break; 74 case 2: sum = B [0]-B [1]; break; 75 case 3: sum = B [0] * B [1]; break; 76 case 4: sum1 = fractDiv2 (B [0], B [1]); break; // call a function when division is performed, the result shows the score 77} 78/* if (sum <0) // The subtraction calculation result cannot be 0 79 {80 j --; 81} */82 if (c [0] = 4) // if the symbol is a division sign, 83 {84 System. out. println (B [0] + a [c [0]-1] + B [1] + "="); 85 System. out. print ("your answer is:"); 86 res = in. next (); 87 if (! Res. equals (sum1) 88 {89 System. out. println ("your answer is incorrect! Correct answer: "+ sum1); 90} 91 else 92 rshu ++; 93} 94 else // If the symbol is not a division sign, 95 {96 System. out. println (B [0] + a [c [0]-1] + B [1] + "="); 97 System. out. print ("your answer is:"); 98 sum2 = in. nextInt (); 99 if (sum2! = Sum) 100 {101 System. out. println ("your answer is incorrect! Correct answer: "+ sum); 102} 103 else104 rshu ++; 105 // System. out. println (sum); 106} 107 System. out. println (r); 108 109} 110} 111 else112 {} 113 System. out. println ("You have correctly answered" + rshu + ""); 114 115} 116 static String fracAdd (int first_numerator, int first_denominator, int second_numrator, int second_denominator) {117 // score addition 118 // The function 119 120 int denominator that needs to be called to obtain the maximum common approx. // the numerator 121 int numerator; // denominator 122 123 if (first_denominato R = second_denominator) // the denominator is simultaneously added with the numerator 124 {125 denominator = first_denominator; 126 numerator = first_numerator + second_numrator; 127} 128 else // otherwise, the comparison numerator 129 {130 denominator = first_denominator * second_denominator; 131 numerator = first_numerator * second_denominator + first_denominator * second_numrator; 132} 133 int gcd = gcd (numerator, denominator); // call The gcd method to score about 134 denominator = denominator/gcd; 135 numerato R = numerator/gcd; 136 if (0 = denominator/numerator) 137 {138 String res = "0"; 139 return res; 140} 141 else142 {143 String zi1 = String. valueOf (numerator); // convert the result to a String and output 144 String zi2 = String. valueOf (denominator); // convert the result to a String and output 145 String zi3 = "/"; // convert the result to a String and output 146 String res2 = zi1 + zi3 + zi2; // convert the result to a String and output 147 148 return res2;} 149 150} 151 static String fracSub (int first_numerator, int first_denominator, int s Econd_numrator, int second_denominator) {152 153 // function 154 155 int denominator; 156 int numerator; 157 158 if (first_denominator = second_denominator) // The denominator is simultaneously added to the numerator 159 {160 denominator = first_denominator; 161 numerator = first_numerator-second_numrator; 162} 163 else // otherwise the same score compared to numerator 164 {165 denominator = first_denominator * second_denominator; 166 numerator = first_numerator * second_denominator-first_de Nominator * second_numrator; 167} 168 int gcd = gcd (numerator, denominator); 169 denominator = denominator/gcd; 170 numerator = numerator/gcd; 171 if (0 = denominator/numerator) 172 {173 String res = "0"; 174 return res; 175} 176 else177 {178 String zi1 = String. valueOf (numerator); 179 String zi2 = String. valueOf (denominator); 180 String zi3 = "/"; 181 String res2 = zi1 + zi3 + zi2; 182 return res2;} 183 184} 185 s Tatic String fracMul (int first_numerator, int first_denominator, int second_numerator, int second_denominator) {186 187 // function 188 int denominator; 189 int numerator; 190 191 192 denominator = first_denominator * second_denominator; 193 numerator = first_numerator * second_numerator; 194 195 int gcd = gcd (numerator, denominator); 196 denominator = denominator/gcd; 197 numerator = numerator/gcd; 1 98 if (0 = denominator/numerator) 199 {200 String res = "0"; 201 return res; 202} 203 else204 {205 String zi1 = String. valueOf (numerator); 206 String zi2 = String. valueOf (denominator); 207 String zi3 = "/"; 208 String res2 = zi1 + zi3 + zi2; 209 return res2;} 210 211} 212 static String fractDiv (int first_numerator, int first_denominator, int second_numerator, int second_denominator) {213 214 // The function 215 216 int d that needs to be called to calculate the maximum public approx. Enominator; 217 int numerator; 218 219 numerator = first_numerator * second_denominator; 220 denominator = optional * second_numerator; 221 222 223 int gcd = gcd (numerator, denominator ); 225 denominator = denominator/gcd; 226 numerator = numerator/gcd; 227 String zi1 = String. valueOf (numerator); 228 String zi2 = String. valueOf (denominator); 229 String zi3 = "/"; 230 String res2 = zi1 + zi3 + zi2; 231 Return res2; 232 233} 234 static String fractDiv2 (int first, int second) {235 236 // The function 237 int denominator = first that needs to be called to calculate the maximum common approx; 238 int numerator = second; 239 int gcd = gcd (numerator, denominator); 240 denominator = denominator/gcd; 241 numerator = numerator/gcd; 242 String zi1 = String. valueOf (numerator); 243 String zi2 = String. valueOf (denominator); 244 String zi3 = "/"; 245 String res2 = zi1 + zi3 + zi2; 246 r Eturn res2; 247} 248 static int gcd (int x, int y) {// calculate the maximum approximate number of 249 int r by Division of the moving phase; 250 while (y! = 0) 251 {252 r = x % y; 253 x = y; 254 y = r; 255} 256 return x; 257 258} 259 260 261}
Running result:
Feeling: It took about four hours to change the original job to an algorithm that can automatically generate the score addition, subtraction, multiplication, division algorithm. Although not perfect, the basic functions are still implemented. I feel very happy.
Note: The program has been written yesterday afternoon, but the school network has been unable to upload the program. Today, there is no way, you can only use a mobile phone to open a Wi-Fi computer and then publish it! T_T