# Elementary school students arithmetic the program No interface Java Edition simple use of some basic Java operations

Source: Internet
Author: User

This is the first experiment in the syllabus of Java in this semester, which simply makes a no-user interface version.

`ImportJava.util.Random;ImportJava.util.Scanner; Public classMathtest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub                Doubleright = 0; DoubleWrong = 0; /*Scanner SC2 = new Scanner (system.in); int num2 = Sc2.nextint ();*/         for(inti = 0; I < 10; i++) {System.out.println ("+ (i+1) +" questions, please enter the answer below: "); //generate two random numbersRandom R1 =NewRandom (); intx = R1.nextint (10) +1; Random R2=NewRandom (); inty = R2.nextint (10) +1; //generate random operator +-*/Random R3 =NewRandom (); intz = R3.nextint (4); Char[] CHS = {' + ', '-', ' * ', '/'}; String Operator=string.valueof (Chs[z]); //Generate Topics        if(Operator.equals ("+") {System.out.println (x+ "+" +y+ "="); Booleanb =Add (x, y); if(b = =true) { Right++; System.out.println ("bingo!"); }Else{wrong++; System.out.println ("Oh~no"); } System.out.println ("The right Answer:" + (x+y)); }Else if(Operator.equals ("-") {System.out.println (x+ "-" +y+ "="); Booleanb =minus (x, y); if(b = =true) { Right++; System.out.println ("bingo!"); }Else{wrong++; System.out.println ("Oh~no"); } System.out.println ("The right Answer:" + (X-y)); }Else if(Operator.equals ("*") {System.out.println (x+ "x" +y+ "="); Booleanb =Times (x, y); if(b = =true) { Right++; System.out.println ("bingo!"); }Else{wrong++; System.out.println ("Oh~no"); } System.out.println ("The right Answer:" + (x*y)); }Else{System.out.println (x+ "÷" +y+ "="); Booleanb =divide (x, y); if(b = =true) { Right++; System.out.println ("bingo!"); }Else{wrong++; System.out.println ("Oh~no"); } System.out.println ("The right Answer:" + ((float) x/(float) (y)); } System.out.println ("-------------------------------"); } System.out.println ("You've done the right thing," +right+ said.); System.out.println ("You've done the wrong" +wrong+ "question."); if(Wrong > 0) {System.out.println ("Correct rate is" + (right/(wrong+right)) *100+ "%"); }Else{System.out.println ("Nothing but .... "); }    }    Private Static BooleanAddintXinty) {//TODO auto-generated Method StubScanner sc =NewScanner (system.in); intNUM1 =Sc.nextint (); intresult = x +y; if(NUM1 = =result) {            return true; }Else{            return false; }            }    Private Static BooleanMinus (intXinty) {//TODO auto-generated Method StubScanner sc =NewScanner (system.in); intNUM1 =Sc.nextint (); intresult = X-y; if(NUM1 = =result) {            return true; }Else{            return false; }            }    Private Static BooleanTimes (intXinty) {//TODO auto-generated Method StubScanner sc =NewScanner (system.in); intNUM1 =Sc.nextint (); intresult = x *y; if(NUM1 = =result) {            return true; }Else{            return false; }        }    Private Static BooleanDivideintXinty) {//TODO auto-generated Method StubScanner sc =NewScanner (system.in); floatNUM1 =sc.nextfloat (); floatresult = (float) x/(float) y; if(NUM1 = =result) {            return true; }Else{            return false; }    }}`

We can see that the method of judging right and wrong uses the direct operator to calculate the result compared with the result of the function definition, and the equal score is not equal to the score.

Problems in programming: When you calculate a division with decimals, the result is wrong at first, and his correct answer is a numeric value to even round it, which is certainly undesirable.

Workaround: The cause is the default int type x and y, which will be coerced into the type conversion in float division. "So his precision is 7 digits after the decimal point."

Attach the final part of the results show.

Elementary school students arithmetic the program No interface Java Edition simple use of some basic Java operations

Related Keywords:

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.