当我没有主要方法时,我的程序如何运行

时间:2016-11-22 09:41:13

标签: java main acm-java-libraries

我正在大学学习java课程,我们正在使用一个名为acm的库。但是当我们编写代码时,我们不会创建一个主要方法。 " public void run(){}"作为主要方法。请解释一下。

这里的例子是我创建的一个程序。

import acm.program.ConsoleProgram;
import acm.util.RandomGenerator;

public class Assignment3 extends ConsoleProgram {

    private static final long serialVersionUID = 1L;
    private RandomGenerator rgen = RandomGenerator.getInstance();

    public void run() {
        final int QUESTION_AMOUNT = 5;

        println("Welcome to the Math Quiz! You have " + QUESTION_AMOUNT + " questions to answer! Good luck!");

        for (int i = 0; i < QUESTION_AMOUNT; i++) {
            askQuestion();
        }

        println("End of the quiz!");
    }

    public void askQuestion() {
        int num1, num2, kidanswer, realanswer;
        String operation = "";
        boolean x = rgen.nextBoolean();

        if (x == true) {
            operation = "+";
        } else {
            operation = "-";
        }

        if (operation == "-") {
            num1 = rgen.nextInt(0, 20);
            num2 = rgen.nextInt(0, num1);
        } else {
            num1 = rgen.nextInt(0, 20);
            num2 = rgen.nextInt(0, 20 - num1);
        }

        String question = ("What is " + num1 + " " + operation + " " + num2 + " = ");

        if (operation == "-") {
            realanswer = num1 - num2;
        } else {
            realanswer = num1 + num2;
        }

        kidanswer = readInt(question);

        int i = 0;
        while (i < 2) {
            if (kidanswer == realanswer) {
                println("That is correct! Well done!");
                break;
            } else {
                kidanswer = readInt("Wrong answer. Please try another answer: ");
                i++;
                if (i == 2) {
                    if (kidanswer == realanswer) {
                        println("That is correct! Well done!");
                    } else {
                        println("Sorry :( Out of tries! The answer was: " + realanswer);

                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

Something 必须提供一个主要方法。在你的情况下,几乎可以肯定的是你的课程扩展的ACM内容(ConsoleProgram),并且在某些时候会调用你的run方法。

答案 1 :(得分:1)

由于: