我知道sumOfMultiples方法本身的工作原理和问题在于主要方法。当我运行它时,没有任何反应,它只是连续运行。我使用netbeans如果有所作为。没有得到输出到我的程序,只是说“正在运行...”
package examp;
public class Main {
public static void main(String[] args) {
Main example = new Main();
System.out.println("The answer is " + example.sumOfMultiples(2, 3));
}
public int sumOfMultiples(int num1, int num2) {
int num1Total = 0;
int num2Total = 0;
//Total of the multiples of the first number.
while (num1 < 1000) {
num1Total = num1Total + num1;
num1 = num1 + num1;
}
//Total of the multiples of the second number.
while (num2 < 1000) {
if (num2 % num1 != 0) { //Makes sure it doesn't add multiples of the first number twice.
num2Total = num2Total + num2;
}
}
return num1Total + num2Total;
}
}
对不起,如果这是一个愚蠢的问题,只是在几分钟前做了一个帐户。
为什么你不声明方法是静态的,并且在不初始化Main实例的情况下使用它? – CSCH