让用户决定何时退出
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."); } }