当我没有 main 方法时,我的程序如何 运行
How does my program run when I don't have a main method
我正在大学学习 java 课程,我们正在使用一个名为 acm 的库。但是当我们编写代码时,我们不会创建 main 方法。 "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);
}
}
}
}
}
}
Something 必须提供一个主要方法。在你的情况下,它几乎肯定会是你的 class 扩展的 ACM 东西(ConsoleProgram
),并且在某些时候会调用你的 run
方法。
因为:
Assignment3 extends ConsoleProgram
ConsoleProgram
extends Program
Program
defines main
.
我正在大学学习 java 课程,我们正在使用一个名为 acm 的库。但是当我们编写代码时,我们不会创建 main 方法。 "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);
}
}
}
}
}
}
Something 必须提供一个主要方法。在你的情况下,它几乎肯定会是你的 class 扩展的 ACM 东西(ConsoleProgram
),并且在某些时候会调用你的 run
方法。
因为:
Assignment3 extends ConsoleProgram
ConsoleProgram
extendsProgram
Program
definesmain
.