2015-10-01 34 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

为了您的其他情况下,你应该要求用户提供另一个输入,使他们不必重新运行该程序,如果他们提出了一个错字。