Perform simple arithmetic operations (Limit: primary level) and arithmetic operations for Primary School
Program function: Implements 300 simple arithmetic operations, which are limited to the primary level.
The Code is as follows:
Import java. util .*;
Public class {
Public static void main (String [] args ){
Int sum = 0;
Int sub = 0;
Int mult = 0;
Float div = 0;
// Keyboard input identifier flag
Int flag = 0;
Wrote input = new partition (System. in );
Flag = input. nextInt ();
Input. close ();
For (int I = 0; I <300; I ++ ){
// Generate two random numbers
Random ran = new Random ();
Int a = ran. nextInt (100 );
Int B = ran. nextInt (100 );
System. out. println ("two random integers are generated:" + a + "," + B );
// Determine the operation to be executed based on the identifier
Switch (flag ){
Case 0: {sum = a + B;
System. out. println ("sum =" + sum );
Break ;}
Case 1: {if (a> B) sub = a-B; // The calculation result cannot be negative because the operation level is limited to the primary level.
Else sub = B-;
System. out. println ("sub =" + sub );
Break ;}
Case 2: {mult = a * B;
System. out. println ("mult =" + mult );
Break ;}
Case 3: {if (B! = 0) div = (float) a/(float) B; // both numbers are integers, and the result is also an integer. Except for all numbers that do not enter 0, need to be forcibly converted to float type
Else System. out. println ("Division zero, division operation not allowed! ");
System. out. println ("div =" + div );
Break ;}
}
}
}
}
There is a bug in the program. Only one operation can be performed at a time, and the identifier cannot be entered again. Currently, 300 arithmetic questions of the same rule are run at a time. Please give me some advice.