2016-05-26 52 views
1

新手在IT世界有一些初學者的問題。 所以,我正在閱讀一些教程,並且遇到了一個我自己無法解決的問題。代碼如下:Java - 如果語句問題 - 變化條件刪除下面的其他語句

boolean gameOver = true; 
    int score = 5000; 
    int levelCompleted = 5; 
    int bonus = 100; 

     if(score == 5000) 
      System.out.println("Your score is 5000"); 

    if(score > levelCompleted){ 
     System.out.println("Score is greater that levelCompleted"); 
     System.out.println("5000 is greater that 5"); 
     System.out.println("Some new text on true condition"); 
     } 

    if(score<5000){ 
     System.out.println("Score is equal to 5000"); 
     } 
     else 
     { 
     System.out.println("Condition is false"); 
     } 

    if((score < 5000) && (score > 1000)){ 
     System.out.println("Both conditions are true"); 
     } 
     else if(score<1000) 
     { 
     System.out.println("Else if condition is true"); 
     } 
     else 
     { 
     System.out.println("None of the condtitions are true!"); 


    if(gameOver == true){ 
     int finalScore = score + (levelCompleted * bonus); 
     System.out.println("Your final score was " + finalScore); 
     } 


    if(gameOver == true){ 
     score = 10000; 
     levelCompleted = 8; 
     bonus = 200; 
     int finalScore= score + (levelCompleted * bonus); 
     System.out.println("Your final score was " + finalScore); 
     } 


    System.out.println(score); 
    } 

    System.out.println(levelCompleted); 

    } 

} 

代碼的輸出上面顯示預期的結果,它們分別是:

Your score is 5000 
Score is greater that levelCompleted 
5000 is greater that 5 
Some new text on true condition 
Condition is false 
None of the condtitions are true! 
Your final score was 5500 
Your final score was 11600 
10000 
8 

但是,當我改變的if/else if/else語句從

如果條件
if ((score < 5000) && (score > 1000)) 

if((score == 5000) && (score > 1000)) 

正如你所看到的,我只是從<更改爲==,並且我得到以下輸出。

Your score is 5000 
Score is greater that levelCompleted 
5000 is greater that 5 
Some new text on true condition 
Condition is false 
Both conditions are true 
5 

那麼,其他兩個if-s發生了什麼?

哪裏是最後兩個「獨立」system.out.println-s?

另外,從現在開始的5個來自哪裏?我想從levelCompleted變量,但如何?

在此先感謝您的幫助和解釋!

+3

缺少一個右大括號檢查您的壓痕。 – Jasper

+0

也是'== true'是多餘的。它就像'* 1'或'+ 0'。注意'p == true'的結果總是等於'p'。 – Pshemo

+0

是的,我知道如果(p == true)與if(p)相同,因爲如果默認情況下預期會返回true,但我只是按照教程的步伐,寫作風格和作爲初學者這種方式要容易得多。事情是,我已經諮詢了oracle官方文檔,還有一些其他可敬的文檔和書籍,並且沒有找到如何解決使用許多if-s,if-s和其他問題的潛在問題的答案。 – newbie

回答

0

缺少}。見Jasper的評論。如果使用Eclipse,請使用Ctrl + Shift + F自動格式化您的代碼。

0

你以後

System.out.println("None of the condtitions are true!");