从控制台询问是或否
//package com.nowjava; /** 来 自 NowJava.com **/ import java.util.Scanner; public class Main { public static void main(String[] argv) throws Exception { String query = "nowjava.com"; System.out.println(askYesOrNo(query)); } public static boolean askYesOrNo(String query) { String yesOrNo = null; boolean validInput = false; while (!validInput) { System.out.println(query); yesOrNo = getConsoleInput(); validInput = yesOrNo.toUpperCase().equals("Y") || yesOrNo.toUpperCase().equals("N"); if (!validInput) { /*来自 NowJava.com - 时代Java*/ System.out .println("Invalid input! Please enter /'Y/' or /'N/'"); } } return yesOrNo.toUpperCase().equals("Y"); }