-1

My code is a guessing game. When the user runs out of guesses or guesses the magic number, I am supposed to ask them if they want to guess a number again. It should be able to run 1000 times as long as the user doesn't enter the sentinel, which is "no." I need to make my code a loop to where it will keep going until the user says "no."

I attempted to do this at the bottom of my code, but got stuck because I feel like I should've done something at the beginning to make it a loop... Can someone show me how I should include this in my code?

uuoo
  • 25
  • 1
  • 1
  • 5
  • 1
    This is a simple loop that you need. Just loop on your actual code until the key word is entered (`no`). This is why methods is a good idea here. Your game should be on a method and your main would only have a loop that while ask to play again and call the `game()` method – AxelH Dec 07 '16 at 06:38
  • 2
    Please see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. Nothing we do to help with the loop will help you until you do the string comparison correctly. – ajb Dec 07 '16 at 06:40
  • 1
    @ScaryWombat I don't agree with the duplicate, yes there is a problem with String comparison, but OP don't know how to create that loop at all. So the main problem (for him) won't be answered in the duplicate, only the side effect will. – AxelH Dec 07 '16 at 06:46
  • 1
    @AxelH Go for it then – Scary Wombat Dec 07 '16 at 06:54

1 Answers1

2

You need to run through a loop as given below:

final String SENTINEL = "no";

# put your required code here

System.out.print(" Would you like to try to guess a number? (Yes or No):");
String answer = scan.next();

while(!answer.equals(SENTINEL)){
     # put your required code here

     # do all your stuff and then ask users' preference again  
     System.out.print("Would you like to try to guess a number? (Yes or No):");
     answer = scan.next();
}

You can also use do-while loop which better matches your need.

final String SENTINEL = "no";
# put your required code here
System.out.print(" Please enter your name: ");
String name = scan.next();  

do{
    # put your required code here

    # do all your stuff and at the end of the loop ask users' preference 
    System.out.print("Would you like to try to guess a number? (Yes or No):");
    String answer = scan.next();
}while(!answer.equals(SENTINEL));

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

AxelH
  • 14,325
  • 2
  • 25
  • 55
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • I would use a do-while with only the question at the end actually ;) This is perfect usage – AxelH Dec 07 '16 at 06:43
  • I agree. do-while loop is the most suitable for OP's scenario. – Wasi Ahmad Dec 07 '16 at 06:44
  • I took the liberty to edit the answer, you asked the question twice in the loop, but you only need to ask at the end before the do-while condition. PS : you should explain the while condition ;) – AxelH Dec 07 '16 at 06:54