代碼給了我一個錯誤,我無法解釋爲什麼會發生這種情況。有人能解釋我做錯了什麼嗎?實際參數long不能通過方法調用轉換轉換爲int
它說long不能轉換爲int,但是當我將一個int轉換爲long時,我無法將x + multx添加爲long。
class MultCalc {
public static int multx(int x, int y) {
if (x == 0) return 0;
if (y == 0) return 0;
else return x + multx(x, y - 1);
}
public static void main(String[] args) {
long x = 77777;
long y = 1234;
MultCalc calc = new MultCalc();
long ans = calc.multx(x, y);
System.out.println(x + " times " + y + " equals " + ans);
}
}
給我的錯誤。
error: method multx in class MultCalc cannot be applied to given types;
long ans = calc.multx(x, y);
^
required: int,int
found: long,long
reason: actual argument long cannot be converted to int by method invocation conversion
1 error
你需要傳入一個int,在你聲明x和y的main函數中。嘗試轉換爲整數或將x和y定義爲int。 – ForeverStudent