我想在Divisors類中打印位於類computeDivisor中的方法「compute」。我如何得到誰是除數?如何在不同的類中打印方法的結果?
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
computeDivisors cD = new computeDivisors();
System.out.println("Enter an integer: ");
int num = s.nextInt();
System.out.println("The divisors of " + num + " are: ");
cD.compute(); //Why doesn't it print?
s.close();
}
-
public void compute(){
for (i = 2; i <= num/2; i++) {
if (num % i == 0) {
System.out.print(i + " , ");
}
}
}
}
你如何將你的變量'num'傳遞給'public void compute()'?它應該是一個參數嗎? – Andreas