Letting the user decide when to quit - Java Language Basics

Java examples for Language Basics:Console

Description

Letting the user decide when to quit

Demo Code

public class NumberPhobia{
  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args)  {
    int number = 2;
    String input;//from   ww  w.  j a  va 2 s. co m

    while (true){
      System.out.println(number + " ");
      System.out.print("Do you want keep counting?" + " (Y or N)");
      input = sc.next();
      if (input.equalsIgnoreCase("N"))
        break;
      number += 2;
    }
    System.out.println("\nWhew! That was close.");
  }
}

Related Tutorials