2015-11-19 80 views
-1

我正在學習使用Java和自己的代碼,我是網絡技術,並想學習如何編碼。我從一個網站叫編程實踐中學習,我被困在一個任務:新手來嵌套if語句

https://programmingbydoing.com/a/twenty-questions.html

下面

是我的代碼,它會編譯,但問題是與嵌套的if語句不能正常工作,請幫助! !

import java.util.Scanner; 
public class twentyQuestions 
    { 
    public static void main(String[] args) 
     { 
     Scanner keyboard = new Scanner(System.in); 
     String question1, question2, guess; 

     System.out.println("TWO QUESTIONS!"); 
     System.out.println("Think of an object, and I will try to guess it."); 
     System.out.println(); 

     System.out.println("Question 1: Is it an animal, vegetable, or mineral?"); 
     question1 = keyboard.next(); 

     System.out.println(); 

     System.out.println("Question 2: Is it bigger than a bread box?"); 
     question2 = keyboard.next(); 


     if (question1.equals("animal")) 

     { 
       if (question2.equals("no")) 

       { 
        guess = "squirrel"; 
       } 
       else { 
        guess = "moose"; 
       } 
     } 


     else if (question1.equals("vegetable")) 

      { 
      if (question2.equals("no")) 
      { 
       guess = "carrot"; 
      } 
      else 
      { 
       guess = "watermelon"; 
      } 
      } 


     else if (question1.equals("mineral")); 


     { 
      if (question2.equals("no")) 
      { 
       guess = "paper clip"; 
      } 
      else 
      { 
       guess = "Camaro"; 
      } 

     } 

     System.out.println("You're thinking of a " + guess); 
     } 

    } 
} 
+0

'嵌套if語句工作不正常'怎麼會這樣?你有多個嵌套的if語句 –

+0

該代碼沒有錯誤符合...它是當你輸入「動物」或蔬菜,你沒有得到它出現在它們下面 –

+0

開始代碼格式在導入語句 – KPrince36

回答

1

首先關閉所有,你需要經過

else if (question1.equals("mineral")) 

刪除分號然後,你需要在if聲明的末尾添加一個最終else塊捕獲輸入不符合任何的三個投入。然後,它將能夠編譯:

... 
    else if (question1.equals("mineral")) 

    { 
     if (question2.equals("no")) { 
      guess = "paper clip"; 
     } else { 
      guess = "Camaro"; 
     } 

    }else{ 
     System.out.println("Invalid input"); 
     return; 
    } 
    ... 
+0

它應該能夠在沒有最後的情況下編譯。 – Raf

+0

不,它不會,因爲在輸入與任何情況都不匹配的情況下,變量猜測不會被初始化 - 即使您知道輸入總是正確的,編譯器也不會允許它 – AJC

+0

不確定爲什麼這個答案已經被降低了,但是,讓我在這裏給你一個幫助,+1,因爲你的答案確實解決了問題並提供瞭解決方法 – Raf

0

謝謝大家,這裏是我的作品感謝大家最終代碼...: 進口java.util.Scanner的;

公共類twentyQuestions { 公共靜態無效的主要(字串[] args) { 掃描器鍵盤=新掃描儀(System.in); String question1,question2,guess =「」;

System.out.println("TWO QUESTIONS!"); 
    System.out.println("Think of an object, and I will try to guess it."); 
    System.out.println(); 

    System.out.println("Question 1: Is it an animal, vegetable, or mineral?"); 
    question1 = keyboard.next(); 

    System.out.println(); 

    System.out.println("Question 2: Is it bigger than a bread box?"); 
    question2 = keyboard.next(); 


    if (question1.equals("animal")) 

    { 
      if (question2.equals("no")) 

      { 
       guess = "squirrel"; 
      } 
      else { 
       guess = "moose"; 
      } 
    } 


    else if (question1.equals("vegetable")) 

     { 
     if (question2.equals("no")) 
     { 
      guess = "carrot"; 
     } 
     else 
     { 
      guess = "watermelon"; 
     } 
     } 


    else if (question1.equals("mineral")) 


    { 
     if (question2.equals("no")) 
     { 
      guess = "paper clip"; 
     } 
     else 
     { 
      guess = "Camaro"; 
     } 

    } 

     else{ 
    System.out.println("Invalid input"); 
    return; 
    } 

    System.out.println("You're thinking of a " + guess); 
    } 

}