1.源代碼
class FangFaDemo{
public static void main(String[] args){
int x = 1 , y = 2;
System.out.println(sum(x,y));
}
}
public static int sum(int a,int b){
return a+b;
}
2.報錯信息
3.問題分析
顯示的是sum方法需要class,重新檢查了一下,發現是class的大括號沒有把sum方法包括起來
4.問題解決
將大括號的位置更正即可成功編譯
class FangFaDemo{
public static void main(String[] args){
int x = 1 , y = 2;
System.out.println(sum(x,y));
}
//原位置
public static int sum(int a,int b){
return a+b;
}
}//把原位置的大括號移動到這裏來
編譯結果:
5.補充
class是類
interface是接口
enum是枚舉
class等關鍵字需要首寫小寫
源文件中必須有一個public 的類名與文件名一致