1

I have not programmed in a while and I am trying to get back into the swing of things and this is how far I have gotten. My question, is how do I loop the second question so that if the response is something besides a yes or no it asks the question again. I have tried putting a loop around the if statement, but whenever I try and get another response from the user it tells me I cannot use the variable, response, to do so. I feel this is an easy fix for I understand looping, but I am having a hard time wrapping my head around this specific issue, thank you in advance.

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"); } } } 
3
  • 2
    Don't use new String("yes"). "yes" will do fine. Commented May 18, 2016 at 18:38
  • 1
    The "swing" of things..heh.. Commented May 18, 2016 at 18:48
  • @Eric but... there's no Swing in this question. Commented May 18, 2016 at 19:17

3 Answers 3

5

There are many ways to do it. Here's the first that came to mind:

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"); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

I also had to bring my boolean statements within the loop but after that it worked like a charm. Thanks so much!
0

First off, to make it easier on yourself, you don't need to use boolean variables. You can simply use the .equals() method to test if the response is equal to yes or no. In this case, it would be easier to use if else statements like this:

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(); } } 

Hope this was helpful for you.

2 Comments

I know, I was just practicing the use of my booleans because before I found out about using them in this way I had no idea you could do so.
I understand. There are so many ways it can be done. There's a world of this stuff out there. Its really good to practice as I have found out in the last year or so
-1

How about

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")); 

1 Comment

i didn't downvote. But your code would loop either once or infinitly because response never changes in it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.