Random Subtraction Arithmetic device

Source: Internet
Author: User

/** Random subtraction operator, the requirements are as follows: The program in turn 10 questions, the user input the answer to the question. The user answers a question, the system prompts the result: answer wrong or correct. After the 10 questions are answered, the system calculates the score and outputs it. If the user wants to end prematurely, you can enter-1 to exit early.
/**Random subtraction operators, the requirements are as follows: The program in turn 10 questions, the user input the answer to the question. The user answers a question, the system prompts the result: answer wrong or correct. After the 10 questions are answered, the system calculates the score and outputs it. If the user wants to end prematurely, you can enter-1 to exit early. ------------------------------------- */ Public classMathtest {/*define static constants to prepare for the following switch*/     Public Static Final intADD = 0;  Public Static Final intSUB = 1;  Public Static Final intMULTI = 2;  Public Static Final intDIV = 3; //The division is inaccurate, and you want to replace it with a double type     Public Static voidMain (string[] args) {System.out.println ("Random subtraction operator, program start");    Operation (); }     Public Static voidoperation () {Scanner scan=NewScanner (system.in); Random Random=NewRandom (); intScore = 0;  for(inti = 0; i<10; i++){            intNUM1 = Random.nextint (10); intnum2 = Random.nextint (10) +1;//plus 1 Avoid the divisor is 0 of the case            intsum = 0; intanswer; intType = Random.nextint (4);//random generation of subtraction 4 options            Switch(type) { CaseAdd:sum= num1+num2; System.out.println ("[" + (I+1) + "]" +num1+ "+" +num2+ "=?");  Break;  CaseSub:sum= num1-num2; System.out.println ("[" + (I+1) + "]" +num1+ "-" +num2+ "=?");  Break;  CaseMulti:sum= num1*num2; System.out.println ("[" + (I+1) + "]" +num1+ "*" +num2+ "=?");  Break;  CaseDiv:sum= num1/num2; System.out.println ("[" + (I+1) + "]" +num1+ "/" +num2+ "=?");  Break; } System.out.println ("Please enter the answer:"); Answer=Scan.nextint (); if(Answer = =sum) {System.out.println ("Answer right, Score:" + (score+=10)); }Else if(Answer = =-1) {System.out.println ("Exit Game");  Break; }Else{System.out.println ("Wrong answer, next question"); }        }    }}
View Code

Random Subtraction Arithmetic device

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.