2014-07-19 40 views
1

沒有宣佈一個布爾值時,聲明下面的代碼:錯誤:在Java中

boolean continue = false; 

返回以下錯誤:

error: not a statement 
    boolean continue = false; 
^

這究竟是爲什麼?我對布爾人非常熟悉。

+0

「繼續「是Java中的保留關鍵字,因此使用alm任何其他名稱都應該解決這個問題。 – Willem

+1

'continue'是一個關鍵字,不能用它作爲變量名稱。 – hobbs

+1

語法高亮編輯器使這類問題更加明顯。 –

回答

2

試試這個:

boolean cont = false; 

或使用其他名稱。問題在於,在Java中,continuekeyword,它不能用作變量名 - 在語言規範中它是正確的here。對於未來的reference這是continue用於:

The continue statement skips the current iteration of a for , while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.

+0

Downvoter:關心評論?這個答案有什麼問題,我該如何改進? –

+1

我認爲你的答案是有效的,這就是爲什麼我接受它,但我不知道爲什麼有人會投票呢? – bradc14

+0

@ bradc14是一個開車的downvoter,或者可能是有人懷恨在心。不公平,但誰在乎... –

0

不能使用繼續作爲一個變量的名字。這是java保留字之一。

0

不能使用continue作爲變量名,因爲它是一個保留字。

the docs

The continue statement skips the current iteration of a for , while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.

你可以告訴大家這是一個關鍵詞,因爲,當你看你的問題,continue有語法高亮應用,像booleanfalse

boolean continue = false; 

這就好比寫

boolean boolean = false; 

boolean false = false; 

這些都顯然是行不通的,所以嘗試其他的東西像continuing

boolean continuing = false;