Test input in while condition - Java Language Basics

Java examples for Language Basics:while

Description

Test input in while condition

Demo Code

import java.util.Scanner;

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

  public static void main(String[] args)
  {//from  w  w w  . j  a  v  a  2 s.c o m
    int number = 2;
    String input = "Y";

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

Related Tutorials