Two-person team: Hanbin, Yin Liangliang
Summary of requirements:
Write a software that automatically generates the arithmetic of a primary school and can determine its correctness
Analysis:
Due to the arithmetic of pupils, it is mainly to generate random number, call subtraction method. Among them, the most important thing is to consider the problem of division, because the division needs to use double or float type.
Some of the code is as follows:
Package fourarithmetic;
Import Java.util.Random;
Import Java.util.Scanner;
public class Fourarithmetic {
/**
* Loop out the number of questions and generate random numbers
*/
public static void Main (string[] args) {
Initialize the error to 0
int right=0;
int wrong=0;
for (int i=1;i<=8;i++) {
System.out.println ("+i+": ");//To do the first question, the loop output
Random r=new random ();//Generate a random number to make a quiz
int A=r.nextint (12) +1;
Random r1=new random ();
int B=r1.nextint (12) +1;
Random r2=new random ();//Generate stochastic operator
int C=r2.nextint (3) +1;
Char chrs[]={' + ', '-', ' * ', '/'};
String chr=string.valueof (Chrs[c]);
Call add to perform an addition operation
if (chr.equals ("+")) {
System.out.println (A + "+" +b+ "=");
Boolean X=add (A, b);
if (x==true) {
SYSTEM.OUT.PRINTLN ("input correct");
right++;
}else{
SYSTEM.OUT.PRINTLN ("calculation error");
wrong++;
}
System.out.println ("The correct answer is:" + (a+b));
}
Call the subtraction method to perform the operation
if (Chr.equals ("-")) {
System.out.println (A + "-" +b+ "=");
Boolean X=sub (A, b);
if (x==true) {
SYSTEM.OUT.PRINTLN ("input correct");
right++;
}else{
SYSTEM.OUT.PRINTLN ("calculation error");
wrong++;
}
System.out.println ("The correct answer is:" + (A-B));
}
Call Multiplication for operations
if (Chr.equals ("*")) {
System.out.println (A + "*" +b+ "=");
Boolean x=multiplication (A, b);
if (x==true) {
SYSTEM.OUT.PRINTLN ("input correct");
right++;
}else{
SYSTEM.OUT.PRINTLN ("calculation error");
wrong++;
}
System.out.println ("The correct answer is:" + (a*b));
}
Call Division Division for Operations
if (Chr.equals ("/")) {
System.out.println (A + "/" +b+ "=");
Boolean x=division (A, b);
if (x==true) {
SYSTEM.OUT.PRINTLN ("input correct");
right++;
}else{
SYSTEM.OUT.PRINTLN ("calculation error");
wrong++;
}
System.out.println ("The correct answer is:" + (Double) A/(double) b));
}
}
System.out.println ("************************");
System.out.println ("In total?") "+right+" problem ");
System.out.println ("Have you done something wrong?") "+wrong+" problem ");
}
The following methods are subtraction, respectively
Private static Boolean add (int a, int b) {
Scanner s=new Scanner (system.in);//System input
int N=s.nextint ();
int res=a+b;
if (n==res) {
return true;
}else{
return false;
}
}
Private static Boolean sub (int a, int b) {
Scanner s=new Scanner (system.in);//System input
int N=s.nextint ();
int res=a-b;
if (n==res) {
return true;
}else{
return false;
}
}
private static Boolean multiplication (int a, int b) {
Scanner s=new Scanner (system.in);//System input
int N=s.nextint ();
int res=a*b;
if (n==res) {
return true;
}else{
return false;
}
}
private static Boolean division (int a, int b) {
Scanner s=new Scanner (system.in);//System input
float n=s.nextfloat ();
float res= (float) A/(float) b;
if (n==res) {
return true;
}else{
return false;
}
}
}
Execution Result:
1th question:
7/3=
2.33333
Calculation error
The correct answer is: 2.3333333333333335
2nd question:
12-10=
2
Enter the correct
The correct answer is: 2
3rd question:
3/7=
0.4285
Calculation error
The correct answer is: 0.42857142857142855
4th question:
9/3=
3
Enter the correct
The correct answer is: 3.0
5th question:
2/10=
0.2
Enter the correct
The correct answer is: 0.2
6th question:
4*5=
20
Enter the correct
The correct answer is: 20
7th question:
2/12=
0.16
Calculation error
The correct answer is: 0.16666666666666666
8th question:
5/7=
0.7142
Calculation error
The correct answer is: 0.7142857142857143
************************
All in all? 4 questions
Did you make a mistake? 4 questions
question to be raised: If you add parentheses, how to write, how to preserve the number of bits in the division, and when to carry the rounding. How to write a program to support the true score of arithmetic and so on, because the problem is strong scalability, so there are many areas to be improved.
Project Source Address:Https://git.coding.net/handsomeman/four_arithmetic.git
Two people team, the pupil's arithmetic