Requirements Analysis Function Requirements:
1, can automatically generate primary school arithmetic topic.
2, the program can accept the user input answers, and determine the right and wrong.
3, give the total number of correct.
Design
1. Define s as a set of 1 to 4 random numbers, using the which (s) statement, Case S is the subtraction of arithmetic respectively. Define two variables in the case statement, A and B, so that they can produce a random number between 0 and 100 for arithmetic.
2, which statement outer layer in the writing of a while statement, so that which can loop multiple times, multiple calculations.
3, define an H record the correct number of questions, the initial value is 0.
Code implementation
Code:
Import Java.util.Scanner;
public class Ruanjian {
public static int Suiji (int num1,int num2) {
int c= (int) num1+ (int) (Math.random () * (NUM2-NUM1));
return C;
}
public static void Main (string[] args) {
int s,a,b,i=1,f=0,h=0;
Double D;
while (i<=5) {
S=suiji (1,4);
i=i+1;
A=suiji (0,100);
B=suiji (0,100);
Switch (S) {
Case 1:
System.out.println (A + "+" +b+ "=");
Scanner reader=new Scanner (system.in);
D=reader.nextint ();
if (d==a+b) {
h++;
}
else{
System.out.println ("The Wrong answer is:" + (a+b));
}
Break
Case 2:
System.out.println (A + "*" +b+ "=");
Scanner reader1=new Scanner (system.in);
D=reader1.nextint ();
if (d==a*b) {
h++;
}
else{
System.out.println ("The Wrong answer is:" + (a*b));
}
Break
Case 3:
System.out.println (A + "-" +b+ "=");
Scanner reader2=new Scanner (system.in);
D=reader2.nextint ();
if (d==a-b) {
h++;
}
else{
System.out.println ("The Wrong answer is:" + (A-B));
}
Break
Case 4:
System.out.println (A + "/" +b+ "=");
Scanner reader3=new Scanner (system.in);
D=reader3.nextint ();
if (d==a/b) {
h++;
}
else{
System.out.println ("Wrong answer:" + (A/b));
}
Break
}
Continue
}
SYSTEM.OUT.PRINTLN ("You have correctly answered the" +h+ "question");
}
}
Program Effect:
PSP Time-consuming statistics
Summarize
For the program of writing ideas in advance written on the manuscript, only spent one night to write the simplest code, but in the process of modification encountered several problems, for break and continue choice was once in trouble, because my code when running multiple calculations, A and B display the same value, I thought it was break and continue didn't have a choice, but after consulting the teacher, I found that the steps I defined for a and B to generate random numbers were written before the while loop. In addition, I hope to be able to list several topics at the same time after the design improvement, so that the user can write the answer at the same time.
Software Engineering (second job)