mercredi 29 mars 2017

JavaScript closing statements

Okay so my code works, however if the user correctly guesses "blue", the line prints "you got the right color!". Then the code should end. However, it brings up another a k.next(); line. How can I prevent this? If you do not understand, here is the code.

import java.util.Scanner;

public class BEassignment11
{
  public static void main (String [] args)
  {
    Scanner k = new Scanner (System.in);
    String color;
    String again;
    
    do 
    {
      
      System.out.println ("Try to guess my favorite color!");
      color = k.next();
      
      if  (color.equalsIgnoreCase ("blue"))
      {    
        System.out.println ("You got the right color!");
        
      }
      
      else
      
        System.out.println ("That is not the right color. Would you like to try again? Yes/No"); 
        again = k.next();
    }
    while (again.equalsIgnoreCase ("yes"));
    
  }
}

Aucun commentaire:

Enregistrer un commentaire