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 Answers3

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");
    }
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
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.

Alex Webb
  • 18
  • 6
  • 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. – Jordan Lee Burnes May 18 '16 at 20:53
  • 1
    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 – Alex Webb May 19 '16 at 01:39
-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"));
scarecrow-
  • 86
  • 1
  • 7