0

我不久前做了一个程序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");
}

}

需要注意的是,我知道这个程序没有优化,我暂时可以接受

4

3 回答 3

2

让它尽可能接近Java。它使用简单的手段,可以1:1翻译成C++。要更改设计,可以在之后完成,特别是如果您对 C++ 比较陌生。

没有像 Java 泛型/C++ 模板这样的技术转变。

于 2013-09-27T14:05:37.917 回答
2

这是关于程序设计的。如果不考虑当前程序的优点(请查看代码审查),好的 OO 设计就是一个好的设计,一个坏的就是一个坏的设计,与语言无关。

于 2013-09-27T13:58:41.470 回答
0

要扩展我的原始评论以及其他人在此处发布的答案...

如果您想自学 C++,并且您已经了解 Java,那么两者非常相似。因此,根据您对 Java 的了解,自学 C++。

你知道如何在 Java 中处理 IO。查阅一些文档并开始使用 C++ 练习 IO,这有点不同。

您知道如何处理 Java 中的方法,并将其与 C++ 进行比较(它们几乎相同)。

但另一方面,如果您想关注效率、组织或优化,您可能应该从使用您最了解的任何语言开始。就目前而言,或者至少在您提出问题的方式上,您似乎正试图在一个范围内完成这两个目标,而选择其中一个并专注于这一目标要好得多。如果您只是简单地复制和粘贴您的代码并修复 Java 和 C++ 之间的语法差异,那么 C++ 版本的代码本质上将与您的代码的 Java 版本一样组织化、高效和优化。

于 2013-09-27T14:11:36.223 回答