我不久前做了一个程序Java
,它本质上是一个数学测验,它根据用户输入随机生成简单的数学问题。现在,我正在自学C++
,我只是想知道,基于效率和组织,转换这个程序的最佳方法是什么。例如,我的方法是否应该是类,它们是否应该留在一个类中,等等。对此的任何帮助将不胜感激!这是我当前的Java
代码
public class MathQuiz {
Scanner s = new Scanner(System.in);
int numberOne;
int numberTwo;
int actualSolution;
int userSolution;
int numberOfQuestions;
int counter = 1;
int temp;
int randomOperation;
int totalCorrect;
int totalIncorrect;
long startTime;
long endTime;
long totalTime;
double percent;
String operation;
String done;
public void mathQuizzer(){
System.out.println("Welcome to Math Quiz!");
System.out.println("What kind of questions would you like to answer?");
System.out.println("Enter + for Addition");
System.out.println("Enter - for Subtraction");
System.out.println("Enter * for Multiplication");
System.out.println("Enter / for Division");
System.out.println("Enter m for Mix");
operation = s.next();
System.out.println("How many questions would you like to answer? ");
numberOfQuestions = s.nextInt();
System.out.println();
startTime = System.currentTimeMillis();
while(counter <= numberOfQuestions){
if(operation.equals("+")){
System.out.println("Question " + counter + ":");
createAdditionEquation();
checkSolution();
counter++;
}
else if(operation.equals("-")){
System.out.println("Question " + counter + ":");
createSubtractionEquation();
checkSolution();
counter++;
}
else if(operation.equals("*")){
System.out.println("Question " + counter + ":");
createMultiplicationEquation();
checkSolution();
counter++;
}
else if(operation.equals("/")){
System.out.println("Question " + counter + ":");
createDivisionEquation();
checkSolution();
counter++;
}
else if (operation.equals("m")){
System.out.println("Question " + counter + ":");
createMixEquation();
checkSolution();
counter++;
}
else{
System.out.println("Sorry invalid operation");
System.exit(0);
}
}
endTime = System.currentTimeMillis();
report();
}
public void createAdditionEquation(){
numberOne = (int)(Math.random() * (100 + 1));
numberTwo = (int)(Math.random() * (100 + 1));
actualSolution = numberOne + numberTwo;
System.out.println(numberOne + " + " + numberTwo + "= ?");
}
public void createSubtractionEquation(){
numberOne = (int)(Math.random() * (100 + 1));
numberTwo = (int)(Math.random() * (100 + 1));
if(numberOne >= numberTwo){
actualSolution = numberOne - numberTwo;
}
else{
numberOne = temp;
numberOne = numberTwo;
numberTwo = temp;
actualSolution = numberOne - numberTwo;
}
System.out.println(numberOne + " - " + numberTwo + "= ?");
}
public void createMultiplicationEquation(){
numberOne = (int)(Math.random() * (10 + 1));
numberTwo = (int)(Math.random() * ((20 - 1) + 1));
actualSolution = numberOne * numberTwo;
System.out.println(numberOne + " * " + numberTwo + "= ?");
}
public void createDivisionEquation(){
numberOne = (int)(Math.random() * (100 + 1));
numberTwo = (int)(1 +(Math.random() * (10 - 1)));
if((numberOne >= numberTwo) && (numberOne%numberTwo == 0)){
System.out.println(numberOne + " / " + numberTwo + "= ?");
actualSolution = numberOne / numberTwo;
}
else{
createDivisionEquation();
}
}
public void createMixEquation(){
randomOperation = (int)(Math.random() * (5 - 1));
System.out.println("Random Operation: " + randomOperation);
if(randomOperation == 0){
createAdditionEquation();
}
else if(randomOperation == 1){
createSubtractionEquation();
}
else if(randomOperation == 2){
createMultiplicationEquation();
}
else
createDivisionEquation();
}
public void checkSolution(){
System.out.println("Answer = ");
userSolution = s.nextInt();
System.out.println();
if(userSolution == actualSolution){
System.out.println("Correct!");
System.out.println("#########################");
System.out.println();
totalCorrect++;
}
else if(userSolution != actualSolution){
System.out.println("Sorry, wrong answer. The correct solution is: " + actualSolution);
System.out.println("#########################");
System.out.println();
totalIncorrect++;
}
}
public void report(){
percent = ((double) totalCorrect / numberOfQuestions) * 100;
totalTime = (endTime - startTime) / 1000;
System.out.println("Report:");
System.out.println("Questions Answered: " + numberOfQuestions);
System.out.println("Total Correct: " + totalCorrect);
System.out.println("Total Incorrect: " + totalIncorrect);
System.out.println("Percent: " + percent + "%");
System.out.println("Time taken: " + totalTime + " seconds");
}
}
需要注意的是,我知道这个程序没有优化,我暂时可以接受