/** 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