如何調用方法並打印其結果?如何調用方法並打印其結果?
public class Test {
public static int main (String args[]){
System.out.println(total);
}
public int numbers (int a, int b){
int total;
total = a + b;
return = total;
}
}
如何調用方法並打印其結果?如何調用方法並打印其結果?
public class Test {
public static int main (String args[]){
System.out.println(total);
}
public int numbers (int a, int b){
int total;
total = a + b;
return = total;
}
}
嘗試此代替:
public class Test {
public static void main (String args[]){
System.out.println(numbers(1, 2));
}
public static int numbers(int a, int b){
int total;
total = a + b;
return total;
}
}
變量範圍限定爲在它們所定義的方法或類,因此,「總」變量是隻能用'數字'方法訪問
公共類測試{
public static void main (String args[]){
System.out.println(numbers(anumber,bnumber));
}
public static int numbers (int a, int b){
int total;
total = a + b;
return total;
}
請努力正確設置您的答案格式。還要提供您所提供的代碼的解釋。解釋爲什麼他們的嘗試失敗,爲什麼你的是正確的。 –
@Sotirios Delimanolis Im sory我在一個hury.Anyway謝謝你的建議,即時通訊新的這一點,所以謝謝你的時間,並建議我 –
它應該是'System.out.println(numbers(someNumber,anotherNumber));'。並且你寫了'return = total;'而不是'return total;'寫了一個錯誤。此外,除非你想創建一個Test類型的對象,否則你的方法需要是'static'。 – Gendarme
關閉主題 - 教程對於學習一些東西很有幫助,但是當涉及到學習語言本身時,我建議您閱讀Herbert Schildt *撰寫的*「Java - 初學者指南」這樣的好書。 – Gendarme