Perform simple arithmetic operations (Limit: primary level) and arithmetic operations for Primary School

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.