The fourth arithmetic program has no operation interface. The java version uses some basic operations in java, and the fourth arithmetic operation is java.

Source: Internet
Author: User

The fourth arithmetic program has no operation interface. The java version uses some basic operations in java, and the fourth arithmetic operation is java.

This is the first experiment in the experiment outline of this semester's java course. Here we have made a simple version without a user interface.

Import java. util. random; import java. util. optional; public class MathTest {public static void main (String [] args) {// TODO Auto-generated method stub double right = 0; double wrong = 0; /* export sc2 = new partition (System. in); int num2 = sc2.nextInt (); */for (int I = 0; I <10; I ++) {System. out. println ("th" + (I + 1) + "question, please enter the answer below:"); // generate two Random numbers Random r1 = new Random (); int x = r1.nextInt (10) + 1; Random r2 = New Random (); int y = r2.nextInt (10) + 1; // generate a Random operator +-*/Random r3 = new Random (); int z = r3.nextInt (4); char [] chs = {'+', '-', '*', '/'}; String Operator = String. valueOf (chs [z]); // generates question if (Operator. equals ("+") {System. out. println (x + "+" + y + "="); boolean B = 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 + "="); boolean B = 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 + "="); boolean B = 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 + "percent" + y + "="); boolean B = 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 have done a total of" + right + "questions. "); System. out. println ("You have made a total of mistakes" + wrong +. "); if (wrong> 0) {System. out. println ("correct rate:" + (right/(wrong + right) * 100 + "%");} else {System. out. println ("cannot be exceeded .... ") ;}} Private static boolean add (int x, int y) {// TODO Auto-generated method stub limit SC = new limit (System. in); int num1 = SC. nextInt (); int result = x + y; if (num1 = result) {return true;} else {return false;} private static boolean minus (int x, int y) {// TODO Auto-generated method stub completion SC = new partition (System. in); int num1 = SC. nextInt (); int result = x-y; if (num1 = result) {return true;} else {return false;} private static boolean times (int x, int y) {// TODO Auto-generated method stub completion SC = new partition (System. in); int num1 = SC. nextInt (); int result = x * y; if (num1 = result) {return true;} else {return false;} private static boolean divide (int x, int y) {// TODO Auto-generated method stub completion SC = new partition (System. in); float num1 = SC. nextFloat (); float result = (float) x/(float) y; if (num1 = result) {return true;} else {return false ;}}}

We can see that the right and wrong methods use direct operators to compare the calculation results with the operation results defined by the function. If they are not equal, they are not scored.

Problems in programming: When the calculation result is a division of decimal places, At first there will be errors in the calculation result. His correct answer is also a number rounded to the even, that must be undesirable.

Solution: The cause is that x and y are int type by default. In float Division, both types must be forcibly converted. In this way, the precision is 7 digits after the decimal point]

The final part of the result is attached.

 

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.