2015-10-01 35 views
-3

因此,我慢慢重新學習java,因爲我已經忘記了大學的所有內容。我一直想知道如果我以怪異的方式做我的循環。我覺得必須有更好的方法來做循環。我很確定我總是讓他們比他們應該的時間更長。無論如何,我遵循http://programmingbydoing.com/a/twenty-questions.html,因爲它使我不得不穀歌很多,迫使我去了解,而不是僅僅複製代碼。我在這20個問題部分(鏈接)。這是我爲它編寫的代碼,它的工作原理。但是有沒有更好的方法可以做到這一點?或者,我可以做些什麼其他可能的方式。 & &是否被視爲嵌套的if語句。Java如果語句 - 初學者

編輯*來想一想。任何批評都是受歡迎的。不一定只是我的循環。如果有什麼我正在做的長版或任何我正在做一個奇怪的方式,請讓我知道。

* edit2。哎呀,對不起。不知道有一個codereview區域。如果我知道它的存在,會在那裏發佈。

import java.util.Scanner; 

public class TwoQuestions 
{ 
    public static void main (String [] args) 


    { 

      Scanner keyboard = new Scanner (System.in); 

      String answerOne, answerTwo; 

      System.out.println("Two questions game!"); 
      System.out.println("Think of an object, and I'll try to guess it"); 

      System.out.println(""); 
      System.out.println(""); 

      System.out.println("Question 1 - Is it an animal, vegetable, or random thing?"); 
      answerOne = keyboard.next(); 

      System.out.println(""); 
      System.out.println("Question 2 - Is it bigger than a breadbox?"); 
      answerTwo = keyboard.next(); 

      if (answerOne.equals("animal") && answerTwo.equals("yes")) 
      { 
       System.out.println("You are thinking of a moose!"); 
      } 

      else if (answerOne.equals("animal") && answerTwo.equals("no")) 
      { 
       System.out.println("You are thinking of a squirrel"); 
      } 

      else if (answerOne.equals("mineral") && answerTwo.equals("yes")) 
      { 
       System.out.println("You are thinking of a Camaro"); 
      } 

      else if (answerOne.equals("mineral") && answerTwo.equals("no")) 
      { 
       System.out.println("You are thinking of a paper clip"); 
      } 

      else if (answerOne.equals("vegetable") && answerTwo.equals("yes")) 
      { 
       System.out.println("You are thinking of a watermelon"); 
      } 

      else if (answerOne.equals("vegetable") && answerTwo.equals("no")) 
      { 
       System.out.println("You are thinking of a carrot"); 
      } 

      else 
      { 
       System.out.println("Please make sure you are spelling correctly, no caps"); 
      } 

    } 
} 
+5

你沒有循環。你只是問兩個問題,然後根據這些答案打印猜測。 –

+3

我投票結束這個問題,因爲它屬於http://codereview.stackexchange.com/ – Andreas

+1

請花時間學習[區別循環和條件](https://docs.oracle的.com/JavaSE的/教程/ JAVA/nutsandbolts/flow.html)。雖然兩種結構都用於控制流程,但它們是不同的。 – MarsAtomic

回答

0

你的條件語句沒有錯,但那些不是循環。

閱讀本的信息,循環:http://www.tutorialspoint.com/java/java_for_loop.htm

爲了您的其他情況下,你應該要求用戶提供另一個輸入,使他們不必重新運行該程序,如果他們提出了一個錯字。