韩老师:
package time_01;
import java.util.*;
public class Hsp_Utility {
//静态属性。。。
private static Scanner scanner = new Scanner(System.in);
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);//包含一个字符的字符串
c = str.charAt(0);//将字符串转换成字符char类型
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static char readChar() {
String str = readKeyBoard(1, false);//就是一个字符
return str.charAt(0);
}
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(10, false);//一个整数,长度<=10位
try {
n = Integer.parseInt(str);//将字符串转换成整数
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static int readInt(int defaultValue) {
int n;
for (; ; ) {
String str = readKeyBoard(10, true);
if (str.equals("")) {
return defaultValue;
}
//异常处理...
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
public static char readConfirmSelection() {
System.out.println("请输入你的选择(Y/N): 请小心选择");
char c;
for (; ; ) {//无限循环
//在这里,将接受到字符,转成了大写字母
//y => Y n=>N
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
//定义了字符串
String line = "";
//scanner.hasNextLine() 判断有没有下一行
while (scanner.hasNextLine()) {
line = scanner.nextLine();//读取这一行
//如果line.length=0, 即用户没有输入任何内容,直接回车
if (line.length() == 0) {
if (blankReturn) return line;//如果blankReturn=true,可以返回空串
else continue; //如果blankReturn=false,不接受空串,必须输入内容
}
//如果用户输入的内容大于了 limit,就提示重写输入
//如果用户如的内容 >0 <= limit ,我就接受
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
复写:
package time_01;
//自己复写Hsp_Utility
import java.util.Scanner;
public class Late_Utility {
private static Scanner scanner = new Scanner(System.in);//为了能让这些静态函数使用,需要写成static
public static void main(String[] args) {
boolean continual = true;
//测试readKeyBoard
System.out.println("测试readKeyBoard()...");
int limit;
boolean blankReturn;
while (continual) {
System.out.print("请输入limit");
while (!scanner.hasNextInt()) {
System.out.println("error,need int");
scanner.next();
}
limit = scanner.nextInt();
System.out.println("blankReturn?(true or false)");
while (!(scanner.hasNext("true") || scanner.hasNext("false"))) {
System.out.println("error,need "true" or "false"");
scanner.next();
}
//问题代码修改结果
//上面是原版,在下面代码编译器给出的更好写法
blankReturn = !"false".equals(scanner.next());
scanner.nextLine();//清除上面注释所说换行符
System.out.print("准备完成,请输入:");
System.out.println(Late_Utility.readKeyBoard(limit, blankReturn));
System.out.println("===========================");
do {
System.out.print("want quit? input "y" or "n":");
if (scanner.hasNext("y")) {
System.out.println("readKeyBoard()测试结束");
continual = false;
} else if (!scanner.hasNext("n")) {
System.out.println("error,need "y" or "n"");
}
} while (!"n".equals(scanner.nextLine()) && continual);//判断,顺便清理
//这里用next()清理的话,会留下换行符
//这里不要写成 while ( continual && !"n".equals(scanner.nextLine()))
//因为如果第一个continual == false,就会直接退出循环,无法达到清理的目的
}
//测试readMenuSelection()
System.out.println("测试readMenuSelection()");
continual = true;
while (continual) {
System.out.print("请输入1-5:");
System.out.println("输入为:" + Late_Utility.readMenuSelection());
//判断是否继续
//熟悉一下逻辑再写一遍,其实可以写成一个成员函数
String choice;
do {
System.out.print("want quit readMenuSelection? "y" or "n"");
if (!scanner.hasNext()) {
System.out.println("user choose to stop input");
break;
} else {
choice = scanner.next();//会留下换行符,影响之后的输入
if ("y".equals(choice)) {
System.out.println("bye_from readMenuSelection().");
continual = false;
} else if (!"n".equals(choice)) {
System.out.println("error, need "y" pr "n"nplease input again");
}
//因为上面注释中所述问题,这里只好直接粗暴清除当前行剩余部分
scanner.nextLine();
}
} while (continual && !"n".equals(choice));//输入不是y也不是n时,继续请求输入
}
//测试
}
private static String readKeyBoard(int limit, boolean blankReturn) {
if (limit < 0) {
return "error: limit = " + limit;
}
String getString = "";
while (scanner.hasNextLine()) {
getString = scanner.nextLine();
if (getString.length() == 0 && !blankReturn) {
System.out.print("error,need one line > 0nplease input again:");
continue;
}
if (getString.length() > limit) {
System.out.println("error,need input's length <= " + limit);
System.out.print("please input again:");
continue;
}
break;
}
return getString;
}
public static char readMenuSelection() {
char getChar;
//Scanner类无法直接读取char,演示如何间接获取char
while ((getChar = readKeyBoard(1, false).charAt(0)) < '1'
|| getChar > '5')
System.out.println("error, need 1-5nplease input again");
return getChar;
}
}



