2015-08-23 27 views
0

這是一個簡單的程序,應該在10分鐘內完成。但是這個錯誤不斷告訴javac編譯器在解析時已到達文件末尾。但是當我在IDE(Net beans)中運行相同的東西時,我得到了結果。爲什麼我的程序不能在javac編譯器中工作,但在IDE中工作

public class beerbottlesong 
    { 
public static void main (String[] args){ 
int x=99;//value given to no. of beer bottles 
while(x>0)//starting while loop 
{ 
    System.out.println (x + "bottles of beer on the wall" + x + " bottles of beer."); 

    System.out.println ("Take one down and pass it around, " + (x-1) + " bottles of beer on the wall"); 
    x--;//decrementing the value of beer bottles 

    }//end while loop 

    }//end main method 

    }//error occured in javac -reached end of file while parsing 
+1

你絕對肯定這些文件是一樣的嗎?如果你遺漏了尾部''' – durron597

+0

,那麼你會得到那個錯誤。評論結束括號比讀取正確的格式要花費更多時間和難度。 – ChiefTwoPencils

+0

如果仍然存在此問題,請嘗試轉到源文件的末尾並添加一行或兩行空白,然後查看是否導致問題消失。雖然這似乎不太可能解決任何問題,但我發現程序在文本文件方面存在問題,而這些文件並沒有以適當的換行符結束。 – ajb

回答

2

這是從一個javac編譯器工作正常。

確保你使用的命令

javac FileName.java 

java FileName 

請類名BeerBottleSong,請遵循正確的命名,約定。


如果你仍然無法運行你的程序,請確保你有類路徑設置。

2

當我編譯這個類,我得到

[email protected]:~$ javac beerbottlesong.java 
[email protected]:~$ ls -l beerbottlesong.* 
-rw-rw-r-- 1 peter peter 850 Aug 23 04:53 beerbottlesong.class 
-rw-rw-r-- 1 peter peter 498 Aug 23 04:53 beerbottlesong.java 
[email protected]:~$ java beerbottlesong 
99bottles of beer on the wall99 bottles of beer. 
Take one down and pass it around, 98 bottles of beer on the wall 
98bottles of beer on the wall98 bottles of beer. 
Take one down and pass it around, 97 bottles of beer on the wall 
0

當運行你的代碼,你不改變你的「瓶子」到「瓶」當一個人。

public static void main(String[] args) { 
    int beerNum = 99; 
    String word = "bottles"; 
    while (beerNum > 0) { 

     System.out.println(beerNum + " " + word + " of beer on the wall"); 
     System.out.println(beerNum + " " + word + " of beer."); 
     System.out.println("Take one down."); 
     System.out.println("Pass it around."); 
     beerNum = beerNum - 1; 
     if (beerNum == 1) { 
      word = "bottle"; // singular, as in ONE bottle. 
     } 
     if (beerNum > 0) { 
      System.out.println(beerNum + " " + word + " of beer on the wall"); 
     } else { 
      System.out.println("No more bottles of beer on the wall"); 
     } // end else 
    }// end while loop 
} // end main method 
相關問題