2013-04-28 62 views
4

這是我用Java寫的第二個腳本,因爲我剛剛開始寫一本書,所以我對這個不可避免的荒謬問題表示歉意。字符串沒有在條件聲明?

我正在寫一個腳本,在牆上唱「99瓶啤酒」,並試圖在循環中聲明一個新的String變量。不幸的是,它不會編譯,因爲字符串被認爲是未聲明的。然後,當我將它從條件中拉出時,仍然在循環中,它工作正常。我在這裏錯過了什麼嗎?

工作:

while (beersLeft >= 0) { 
     String sOrNot = ""; 
     if (beersLeft > 1) { 
      sOrNot = "s"; 
     } else { 
      sOrNot = ""; 
     } 
     System.out.println(beersLeft+" bottle"+sOrNot+" of beer on the wall, "+beersLeft+" bottle"+sOrNot+" of beer!"); 
     System.out.println("Take one down, pass it around, "+beersLeft+" bottle"+sOrNot+" of beer on the wall!"); 
     System.out.println(); 

     beersLeft = beersLeft-1; 
    } 

不工作

while (beersLeft >= 0) { 
     if (beersLeft > 1) { 
      String sOrNot = "s"; 
     } else { 
      String sOrNot = ""; 
     } 
     System.out.println(beersLeft+" bottle"+sOrNot+" of beer on the wall, "+beersLeft+" bottle"+sOrNot+" of beer!"); 
     System.out.println("Take one down, pass it around, "+beersLeft+" bottle"+sOrNot+" of beer on the wall!"); 
     System.out.println(); 

     beersLeft = beersLeft-1; 
    } 

錯誤:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any> 
at ch01.BottlesOfBeer.main(BottlesOfBeer.java:21) 
+0

上面'if(beersLeft> 1){'put'String sOrNot =「」;'並刪除else語句,閱讀下面的答案爲什麼**爲什麼** – earcam 2013-04-28 01:30:41

+0

哈哈我不需要閱讀爲什麼,作爲一旦你說我在內部面對面,因爲它顯而易見的冗餘。感謝您指出了這一點。 – 2013-04-28 01:34:36

+0

您還必須從'if'中的sOrNot中刪除'String',因爲這仍然會在if範圍內創建名爲sOrNot的第二個變量。 – earcam 2013-04-28 01:37:18

回答

6

如果您在定義字符串if語句塊將不在範圍內。

3

這與範圍界定有關。

當你在if語句之外聲明它時,聲明對整個循環是可見的。

但是,當你在if語句塊中聲明它時,它們只能從內部看到。這就是爲什麼你以後會得到未聲明的變量錯誤。

一個簡單的方法把這個就是聲明只看到最內層設置,它是在。

5

的問題涉及到可變範圍內聲明的括號{}的。在不工作的例子中,你在if和else的範圍內聲明瞭字符串。所以,當你嘗試打印這些值時,它們超出了範圍。

if (beersLeft > 1) { 
    String sOrNot = "s"; 
    // sOrNot scope ends 
} else { 
    String sOrNot = ""; 
    // sOrNot scope ends 
} 
// sOrNot does not exist in this scope. 
System.out.println(beersLeft+" bottle"+sOrNot+" of beer on the wall, "+beersLeft+" bottle"+sOrNot+" of beer!"); 

工作實例工作,因爲當字符串在循環的頂部聲明,它在整個事情的範圍。

while (beersLeft >= 0) { 
    // has scope for rest of loop 
    String sOrNot = ""; 
     .... 
} 

這種行爲看起來不直觀,但它可以讓你做這樣的事情:

for (int i = 0; i < 10; i++) { 
    // do something 
} 
// The previous i is out of scope, so creates a new one 
// without having the names "clash" 
for (int i = 5; i >= 0; i--) { 
    // do something else 
} 
+1

它看起來有點不直觀!我習慣於PHP,其中循環有自己的作用域,但條件會使它浮出水面。 – 2013-04-28 01:37:46

+0

@Lavalamp先生不知道!這當然解釋了你的困惑。 – 2013-04-28 01:39:19

3

括號定義範圍。在第二個版本中,變量sOrNot未定義,因爲它僅在if語句的範圍內定義。

while (beersLeft >= 0) { 
    if (beersLeft > 1) { 
     String sOrNot = "s"; 
    } else { 
     String sOrNot = ""; 
    } 

    // sOrNot out of scope here - compilation error! 
    System.out.println(beersLeft+" bottle"+sOrNot+" of beer on the wall, "+beersLeft+" bottle"+sOrNot+" of beer!"); 
    ... 
} 

在第一VERSON sOrNot在範圍

2

這是值得Java那樣,以避免錯誤。這個想法是,如果你只給一個條件語句中的sOrNot分配一個值,而不是另一個語句,那麼程序可以運行它的過程,並且永遠不會分配sOrNot值,但它仍然會嘗試打印。如果發生這種情況,程序將會出錯。正如其他人所說,這是範圍問題,但按照我剛剛定義的方式,新程序員理解起來似乎更容易。

希望這會有所幫助!

0

在if語句內聲明的String sOrNot只屬於IF語句,不能在其外部使用。