2016-05-18 153 views
1

我没有编程,我试图回到事物的摆动,这是我得到了多少。我的问题是,我如何循环第二个问题,以便如果答案除了是或否,则再次提出问题。我曾试着围绕if语句进行循环,但每当我尝试获得用户的另一个响应时,它都会告诉我不能使用变量响应来这样做。我觉得这对于我理解循环来说是一个简单的解决方案,但是我很难在这个特定的问题上进行包装,感谢您的提前。Java中的循环逻辑

import java.util.Scanner; 
public class Practice { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Welcome to my simulation, please enter your name"); 
     String name = input.nextLine(); 
     System.out.println("Welcome " + name + " would you like to play a game?"); 
     String response = input.nextLine(); 


     boolean yes = new String("yes").equals(response.toLowerCase()); 
     boolean no = new String("no").equals(response.toLowerCase()); 


     if (yes){ 
      System.out.println("Which game woudl you like to play?"); 
     }else if (no){ 
      System.out.println("Fine then, have a good day!"); 
     } 
     else{ 
      System.out.println("please enter either yes or no"); 
     } 
    } 

} 
+2

请勿使用'new String(“yes”)'。 '“是”'会做得很好。 – shmosel

+1

东西的“摆动”..呃.. – Eric

+0

@Eric但是......在这个问题上没有Swing。 – shmosel

回答

5

有很多方法可以做到这一点。下面是浮现在脑海的第一个:

while (true) { 
    response = input.nextLine().toLowerCase(); 
    if (response.equals("yes") { 
     System.out.println("Which game woudl you like to play?"); 
     break; 
    } else if (response.equals("no") { 
     System.out.println("Fine then, have a good day!"); 
     break; 
    } else { 
     System.out.println("please enter either yes or no"); 
    } 
} 
+0

我也必须在循环中引入布尔语句,但在此之后它就像魅力一样工作。非常感谢! –

-1

如何

do{ 
    String response = input.nextLine(); 
    boolean yes = new String("yes").equals(response.toLowerCase()); 
    boolean no = new String("no").equals(response.toLowerCase()); 
    if (yes){ 
     System.out.println("Which game woudl you like to play?"); 
    }else if (no){ 
     System.out.println("Fine then, have a good day!"); 
    } 
    else{ 
     System.out.println("please enter either yes or no"); 
    } 
}while(!response.equals("yes") && !response.equals("no")); 
+0

我很想知道为什么我得到一个downvote –

+2

我没有downvote。但是你的代码会循环一次或无限次,因为响应永远不会改变它。 –

+0

@ 911didbush好抓! –

-4
while(response =! yes || response || no) 

这应该帮助!

+0

http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – sinclair

+0

'=!'不是我知道的任何语言的有效运算符。此外,这似乎是其他方面令人难以置信的,极好的,迷人的错误。在回答关于它的任何其他问题之前,我建议阅读[关于Java的这个基本教程](https://docs.oracle.com/javase/tutorial/getStarted/application/index.html)。 –

0

首先,使其更容易对自己,你并不需要使用boolean变量。您可以简单地使用.equals()方法来测试响应是否等于是或否。在这种情况下,如果else语句如下所示,将更容易使用:

if (response.equals("Yes")) 
{ 
    System.out.println("Which game woudl you like to play?"); 
} 

else if (response.equals("No")) 
{  
    System.out.println("Fine then, have a good day!"); 
} 

else if (!response.equals("Yes") && !response.equals("No")) 
{ 
    while (!response.equals("Yes") && !response.equals("No")) 
    { 
     System.out.println("please enter either yes or no"); 
     response = input.nextLine(); 
    } 
} 

希望这对您有所帮助。

+0

我知道,我只是在练习使用我的布尔人,因为在我发现以这种方式使用它们之前,我不知道你可以这样做。 –

+1

我明白了。有很多方法可以完成。这里有一个世界。正如我在过去一年左右发现的那样,练习真的很好 –