集册 Java实例教程 让用户决定何时退出

让用户决定何时退出

欢马劈雪     最近更新时间:2020-01-02 10:19:05

480
让用户决定何时退出

public class NumberPhobia{

  static Scanner sc = new Scanner(System.in);


  public static void main(String[] args)  {
  /*
  n o w    j a v a  . c o m
  */

    int number = 2;

    String input;


    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;/*来自 n o w    j a v a  . c o m*/

    }

    System.out.println("\nWhew! That was close.");

  }

}