Re: [問題]請問我哪裡打錯了

看板java作者 (判判)時間15年前 (2009/06/03 17:26), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《peter11016 (邵ㄎㄡ)》之銘言: : public class test : { : public static void main(String[] args) : { : int i=star(5); : System.out.println(i+"stars printed"); : } : } : public star(int n) : { : int j; : for(j=1;j<=2*n;j++) : System.out.println("*"); : System.out.println("\n"); : return 2*n; : } : 怎麼出現這樣的錯誤class,interface,orenum expected public class test { public static void main(String[] args) { test a= new test(); int i=a.star(5); System.out.println(i+" stars printed"); } public int star(int n) { int j; for(j=1;j<=2*n;j++) System.out.println("*"); System.out.println("\n"); return 2*n; } } 這樣就沒有錯誤error了 出來的結果是一行一個* 共10行 star那裏你需要用一個物件,去call method (@@對這些名詞我不太會分只會寫,有誤否?) 而且,star你回傳的是int 在method開頭也得寫上int 另外,你的method要包在class裡面才對 一個檔案只能有一個public class -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.234

06/03 20:01, , 1F
一個檔案只能有一個「public class」
06/03 20:01, 1F
感謝提出修正^^

06/03 20:04, , 2F
喔 感謝有時候就是少根筋
06/03 20:04, 2F
※ 編輯: alvin781205 來自: 118.168.67.88 (06/04 03:09)
文章代碼(AID): #1A9a72sU (java)
文章代碼(AID): #1A9a72sU (java)