临渊羡鱼,不如退而结网
输入输出 输入 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import java.util.Scanner;public class InputTest { public static void main (String[] args) { Scanner in = new Scanner (System.in); System.out.println("What is your name?" ); String name = in.nextLine(); System.out.println("How old are you?" ); int age = in.nextInt(); System.out.println("Hello, " + name + ". Next yoar, you'll be " + (age + 1 )); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.base.learn;import java.util.Scanner;public class ScannerTest { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入你的网名:" ); String name = scanner.next(); System.out.println("请输入你的年龄:" ); int age = scanner.nextInt(); System.out.println("请输入你的体重:" ); double weight = scanner.nextDouble(); System.out.println("你是否单身(true / false):" ); boolean isSingle = scanner.nextBoolean(); System.out.println("请输入的你的性别:" ); char gender = scanner.next().charAt(0 ); System.out.println("你的基本信息如下:" ); System.out.println("网名:" + name + "\n年龄:" + age + "\n体重:" + weight + "\n单身:" + isSingle + "\n性别:" + gender); scanner.close(); } }
输出 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class OutputTest { public static void main (String[] args) { System.out.print("A," ); System.out.print("B," ); System.out.print("C," ); System.out.println(); System.out.println("END" ); double d = 1230000000 ; System.out.println(d); double d1 = 3.1415926 ; System.out.printf("%.2f\n" , d1); System.out.printf("%.3f\n" , d1); int n = 12345000 ; System.out.printf("n=%d, hex=%08x" , n, n); } }
Java的格式化功能提供了多种占位符,可以把各种数据类型“格式化”成指定的字符串 注意,由于%表示占位符,因此,连续两个%%表示一个%字符本身
占位符 说明 %d 格式化输出整数 %x 格式化输出十六进制整数 %f 格式化输出浮点数 %e 格式化输出科学计数法表示的浮点数 %s 格式化字符串
随机数 如何产生一个指定范围的随机整数?
Math 类的 random() 的调用,会返回一个 [0,1) 范围的一个 double 型值
Math.random() * 100 —> [0,100) (int)(Math.random() * 100) —> [0,99] (int)(Math.random() * 100) + 5 —-> [5,104] 如何获取 [a,b] 范围内的随机整数呢?(int)(Math.random() * (b - a + 1)) + a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.base.learn;public class MathRandomTest { public static void main (String[] args) { double d1 = Math.random(); System.out.println(d1); int num1 = (int ) (Math.random() * 101 ); int num2 = (int ) (Math.random() * 100 ) + 1 ; int num3 = (int ) (Math.random() * (15 - 4 + 1 )) + 4 ; System.out.println(num3); } }
件语句 if-else 1 2 3 4 5 6 7 8 9 10 11 class IfTest { public static void main (String[] args) { int n = 70 ; if (n >= 60 ) { System.out.println("及格了" ); } else { System.out.println("END" ); } } }
当 if 语句只有一行语句时,可以省略花括号 {}、但是不推荐忽略花括号的这种写法
1 2 3 4 5 6 7 8 9 10 class IfTest { public static void main (String[] args) { int n = 50 ; if (n >= 60 ) System.out.println("及格了" ); else System.out.println("END" ); } }
多个if ... esle if ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class IfTest1 { public static void main (String[] args) { int n = 70 ; if (n > 90 ) { System.out.println("优秀" ); } else if (n >= 60 ) { System.out.println("及格" ); } else { System.out.println("挂科" ); } System.out.println("END" ); } }
if 条件语句从上到下执行
1 2 3 4 5 6 7 8 9 10 11 12 13 public class Main { public static void main (String[] args) { int n = 100 ; if (n >= 60 ) { System.out.println("及格了" ); } else if (n >= 90 ) { System.out.println("优秀" ); } else { System.out.println("挂科了" ); } } }
浮点数在计算机中常常无法精确计算,并且计算可能出现误差,因此判断浮点数相等用 == 判断不靠谱
1 2 3 4 5 6 7 8 9 10 11 12 class IfTest2 { public static void main (String[] args) { double x = 1 - 9.0 / 10 ; System.out.println(x); if (x == 0.1 ) { System.out.println("x is 0.1" ); } else { System.out.println("x is not 0.1" ); } } }
正确的方法是利用差值小于某个临界值来判断
1 2 3 4 5 6 7 8 9 10 11 12 class IfTest2 { public static void main (String[] args) { double x = 1 - 9.0 / 10 ; System.out.println(x); if (Math.abs(x - 0.1 ) < 0.00001 ) { System.out.println("x is 0.1" ); } else { System.out.println("x is not 0.1" ); } } }
在 Java 中,判断类型的变量值是否相等,可以使用 == 运算符。但是,判断引用类型的变量是否相等, == 表示”引用的是否相等”,或者说,是否指向同一个对象
例如,下面的两个String类型,它们内容是相同的,但是分别指向不同的对象,用 == 来判断,结果为 false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Main { public static void main (String[] args) { String s1 = "hello" ; String s2 = "HELLO" .toLowerCase(); System.out.println(s1); System.out.println(s2); if (s1 == s2) { System.out.println("s1 = s2" ); } else { System.out.println("s1 != s2" ); } } }
要判断引用类型的值是否相等,必须使用 equals() 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Main { public static void main (String[] args) { String s1 = "hello" ; String s2 = "HELLO" .toLowerCase(); System.out.println(s1); System.out.println(s2); if (s1.equals(s2)) { System.out.println("s1 = s2" ); } else { System.out.println("s1 != s2" ); } } }
要避免 NullPointerException 错误,可以利用短路运算符 &&
1 2 3 4 5 6 7 8 public class Main { public static void main (String[] args) { String s1 = null ; if (s1 != null && s1.equals("hello" )) { System.out.println("hello" ); } } }
案例 心率检测,成年人心率的正常范围是每分钟60-100次。体检时,如果心率不在此范围内,则提示需要做进一步的检查
1 2 3 4 5 6 7 8 9 10 public class IfElseTest1 { public static void main (String[] args) { int heartBeats = 110 ; if (heartBeats < 60 || heartBeats > 100 ) { System.out.println("你需要进一步检查" ); } System.out.println("体检结束" ); } }
案例 定义一个整数,判定是偶数还是奇数
1 2 3 4 5 6 7 8 9 10 public class IfElesTest2 { public static void main (String[] args) { int a = 11 ; if (a % 2 == 0 ) { System.out.println(a + "是偶数" ); } else { System.out.println(a + "是奇数" ); } } }
案例 考试成绩判定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class IfElesTest3 { public static void main (String[] args) { int score = 55 ; if (score == 100 ) { System.out.println("奖励一辆跑车" ); } else if (score > 80 ) { System.out.println("奖励一辆山地车" ); } else if (score > 60 ) { System.out.println("奖励环球影城玩一日游" ); } else { System.out.println("胖揍一顿" ); } } }
案例 三个数大小比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class IfElesTest4 { public static void main (String[] args) { int num1 = 22 , num2 = 32 , num3 = 12 ; if (num1 >= num2){ if (num3 >= num1) System.out.println(num2 + "-" + num1 + "-" + num3); else if (num3 <= num2) System.out.println(num3 + "-" + num2 + "-" + num1); else System.out.println(num2 + "-" + num3 + "-" + num3); }else { if (num3 >= num2) System.out.println(num1 + "-" + num2 + "-" + num3); else if (num3 <= num1) System.out.println(num3 + "-" + num1 + "-" + num2); else System.out.println(num1 + "-" + num3 + "-" + num2); } } }
switch case 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class SwitchTest { public static void main (String[] args) { int option = 2 ; switch (option) { case 1 : System.out.println("Selected 1" ); break ; case 2 : System.out.println("Selected 2" ); break ; case 3 : System.out.println("Selected 3" ); break ; default : System.out.println("Selected 4" ); break ; } } }
switch 语句还可以匹配字符串。字符串匹配时,是比较”内容相等”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class SwitchTest { public static void main (String[] args) { String option = "Java" ; switch (option) { case "Python" : System.out.println("Python" ); break ; case "C" : System.out.println("C" ); break ; case "Java" : System.out.println("Java" ); break ; default : System.out.println("Go" ); break ; } } }
案例 使用 switch-case 实现:对学生成绩大于 60 分的,输出“合格”。低于 60 分的,输出“不合格”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.base.learn;import java.util.Scanner;public class SwitchCaseTest3 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入成绩:" ); int score = scanner.nextInt(); switch (score / 60 ) { case 0 : System.out.println("不及格" ); break ; case 1 : System.out.println("及格了" ); break ; default : System.out.println("输入的成绩有误" ); break ; } scanner.close(); } }
利用 switch 的穿透性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 package com.base.learn;import java.util.Scanner;public class SwitchCaseTest4 { public static void main (String[] args) { Scanner scan = new Scanner (System.in); System.out.println("请输入2023年的month:" ); int month = scan.nextInt(); System.out.println("请输入2023年的day:" ); int day = scan.nextInt(); int sumDays = 0 ; switch (month){ case 12 : sumDays += 30 ; case 11 : sumDays += 31 ; case 10 : sumDays += 30 ; case 9 : sumDays += 31 ; case 8 : sumDays += 31 ; case 7 : sumDays += 30 ; case 6 : sumDays += 31 ; case 5 : sumDays += 30 ; case 4 : sumDays += 31 ; case 3 : sumDays += 28 ; case 2 : sumDays += 31 ; case 1 : sumDays += day; } System.out.println(month + "月" + day + "日是2023年的第" + sumDays + "天" ); scan.close(); } }
拓展 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 package com.base.learn;import java.util.Scanner;public class SwitchCaseTest5 { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入year:" ); int year = scanner.nextInt(); System.out.println("请输入month:" ); int month = scanner.nextInt(); System.out.println("请输入day:" ); int day = scanner.nextInt(); int sumDays = 0 ; switch (month) { case 12 : sumDays += 30 ; case 11 : sumDays += 31 ; case 10 : sumDays += 30 ; case 9 : sumDays += 31 ; case 8 : sumDays += 31 ; case 7 : sumDays += 30 ; case 6 : sumDays += 31 ; case 5 : sumDays += 30 ; case 4 : sumDays += 31 ; case 3 : sumDays += 28 ; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ) { sumDays++; } case 2 : sumDays += 31 ; case 1 : sumDays += day; } System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + sumDays + "天" ); scanner.close(); } }
案例 指定一个月份,输出该月份对应的季节
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package com.base.learn;import java.util.Scanner;public class SwitchCaseTest33 { public static void main (String[] args) { Scanner input = new Scanner (System.in); System.out.println("请输入月份:" ); int month = input.nextInt(); switch (month) { case 1 : case 2 : case 12 : System.out.println("冬季" ); break ; case 3 : case 4 : case 5 : System.out.println("春季" ); break ; case 6 : case 7 : case 8 : System.out.println("夏季" ); break ; case 9 : case 10 : case 11 : System.out.println("秋季" ); break ; default : System.out.println("你输入得月份有误" ); break ; } input.close(); } }
if-else与switch-case比较 结论:凡是使用switch-case的结构都可以转换为if-else结构。反之,不成立。 开发经验:如果既可以使用switch-case,又可以使用if-else,建议使用switch-case。因为效率稍高。 细节对比:if-else语句优势if语句的条件是一个布尔类型值,if条件表达式为true则进入分支,可以用于范围的判断,也可以用于等值的判断,使用范围更广。 switch语句的条件是一个常量值(byte,short,int,char,枚举,String),只能判断某个变量或表达式的结果是否等于某个常量值,使用场景较狭窄。 switch语句优势当条件是判断某个变量或表达式是否等于某个固定的常量值时,使用if和switch都可以,习惯上使用switch更多。因为效率稍高。当条件是区间范围的判断时,只能使用if语句。 使用switch可以利用穿透性,同时执行多个分支,而if…else没有穿透性。 循环结构 while 循环 1 2 3 4 5 6 7 8 9 10 11 class WhlieTest { public static void main (String[] args) { int sum = 0 ; int x = 1 ; while (x <= 100 ) { sum += x; x++; } System.out.println(sum); } }
do-while 循环 do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次
1 2 3 4 5 6 7 8 9 10 11 class WhlieTest1 { public static void main (String[] args) { int sum = 0 ; int x = 10 ; do { sum += x; x++; }while (x < 10 ); System.out.print(sum); } }
for 循环 1 2 3 4 5 6 7 8 9 class ForTest { public static void main (String[] args) { int sum = 0 ; for (int i = 1 ; i <= 100 ; i++) { sum += i; } System.out.println(sum); } }
对整形数组的所有元素求和,可以使用 for 循环实现
1 2 3 4 5 6 7 8 9 10 11 class ForTest1 { public static void main (String[] args) { int sum = 0 ; int [] ns = {1 , 4 , 6 , 8 , 11 , 23 , 35 }; for (int i = 0 ; i < ns.length; i++) { System.out.println(i + "," + ns[i]); sum += ns[i]; } System.out.println(sum); } }
灵活使用 for 循环,for 循环还可以缺少初始化语句,循环条件和每次迭代更新语句,如下 通常不建议这样写,某些情况下,是可以省略 for 循环的某些语句
1 2 3 for (int i=0 ; ; i++){}
for each 循环 for 循环经常用来遍历数组,因为通过计数器可以根据索引来访问数组的每个元素,但是,很多时候,我们实际上真正想要访问的数组每个元素的值,Java 还提供了另一种 for each 循环,它可以更简单的遍历数组。
1 2 3 4 5 6 7 8 class ForTest2 { public static void main (String[] args) { int [] ns = {1 , 4 , 6 , 8 , 12 , 29 , 18 }; for (int i : ns) { System.out.println(i); } } }
break 关键字 break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块
1 2 3 4 5 6 7 8 9 10 11 12 class BreakTest { public static void main (String[] args) { int sum = 0 ; for (int i = 1 ; ; i++){ sum += i; if (i == 100 ){ break ; } } System.out.println(sum); } }
break 语句通常都是配合 if 语句使用,特别注意,break 语句总是能跳出自己所在的那层循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class BreakTest1 { public static void main (String[] args) { for (int i=1 ; i<=10 ; i++) { System.out.println("i = " + i); for (int j=1 ; j<=10 ; j++) { System.out.println("j = " + j); if (j >= i){ break ; } } System.out.println("---------" ); } } }
continue 关键字 break 会跳出当前循环,也就是整个循环都不会执行了。而 continue 则是提前结束本次循环,直接继续执行下次循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class ContinueTest { public static void main (String[] args) { int [] ns = {1 , 4 , 6 , 8 }; for (int x : ns) { if (x == 6 ) { continue ; } System.out.println(x); } } }
使用 label 标签 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class BreakTest1 { public static void main (String[] args) { inner: for (int i = 0 ; i < 3 ; i++){ for (int j = 0 ; j < 3 ; j++){ if (i == 1 && j == 1 ) break inner; System.out.println(i + ", " + j); } } System.out.println("结尾" ); } }
练习题 最大公约数,最小公倍数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import java.util.Scanner;class ForTest { public static void main (String[] args) { Scanner scan = new Scanner (System.in); System.out.println("请输入第一个正整数" ); int m = scan.nextInt(); System.out.println("请输入第二个正整数" ); int n = scan.nextInt(); int min = (m <= n)? m : n; for (int i=min; i >= 1 ; i--){ if (m % i ==0 && n % i == 0 ){ System.out.println("最大公约数为:" + i); break ; } } int max = (m >= n)? m : n; for (int i = max; i <= m * n; i++){ if (i % m == 0 && i % n == 0 ){ System.out.println("最小公倍数为:" + i); break ; } } } }
水仙花 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class ForTest2 { public static void main (String[] args) { for (int i = 100 ; i <= 999 ; i++){ int a = i / 100 ; int b = i % 100 / 10 ; int c = i % 10 ; if (a*a*a + b*b*b + c*c*c == i){ System.out.println("此数值满足水仙花:" + i); } } } }
计算100内所有偶数的和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class DoWhlieTest { public static void main (String[] args) { int number = 1 ; int sum = 0 ; int count = 0 ; do { if (number % 2 == 0 ){ System.out.println(number); sum += number; count++; } number++; }while (number <= 100 ); System.out.println("总和为:" + sum); System.out.println("个数为:" + count); } }
不确定整数的个数统计 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.util.Scanner;class XunTest { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int positive_int = 0 ; int negative_int = 0 ; while (true ){ int number = scan.nextInt(); if (number > 0 ){ positive_int++; }else if (number < 0 ){ negative_int++; }else { break ; } } System.out.println("正整数的个数:" + positive_int); System.out.println("负整数的个数:" + negative_int); } }
循环嵌套例子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 class ForForTest { public static void main (String[] args) { for (int i = 1 ; i < 6 ; i++){ for (int j = 1 ; j <= 10 ; j++){ System.out.print("*" ); } System.out.println(); } for (int i = 1 ; i <= 6 ; i++){ for (int j = 1 ; j <= i; j++){ System.out.print("*" ); } System.out.println(); } for (int i = 1 ; i <= 6 ; i++){ for (int j = 1 ; j <= 6 -i; j++){ System.out.print("*" ); } System.out.println(); } for (int i = 1 ; i <= 9 ; i++){ for (int j = 1 ; j <= i; j++){ System.out.print(j + "*" + i + "=" + j*i + "\t" ); } System.out.println(); } } }
质数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class PrimeNumberTest { public static void main (String[] args) { boolean isFlag = true ; for (int i = 2 ; i <= 100 ; i++){ for (int j = 2 ; j < i; j++){ if (i % j == 0 ){ isFlag = false ; } } if (isFlag == true ){ System.out.println(i); } isFlag = true ; } } }
质数查找优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class PrimeNumberTest { public static void main (String[] args) { boolean isFlag = true ; int count = 0 ; long start = System.currentTimeMillis(); for (int i = 2 ; i <= 100000 ; i++){ for (int j = 2 ; j <= Math.sqrt(i); j++){ if (i % j == 0 ){ isFlag = false ; break ; } } if (isFlag == true ){ count++; } isFlag = true ; } long end = System.currentTimeMillis(); System.out.println("质数的个数为:" + count); System.out.println("所花费的时间为:" + (end - start)); } }
最后的总结 家庭收支记账练习 Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public class FamilyAccount { public static void main (String[] args) { String details = "收支\t账户金额\t收支金额\t说 明\n" ; int balance = 10000 ; boolean isFlag = true ; do { System.out.println("\n-----------------家庭收支记账软件-----------------\n" ); System.out.println(" 1 收支明细" ); System.out.println(" 2 登记收入" ); System.out.println(" 3 登记支出" ); System.out.println(" 4 退 出\n" ); System.out.print(" 请选择< 1~4 > " ); char menu = Utility.readMenuSelection(); switch (menu){ case '1' : System.out.println("-----------------当前收支明细记录-----------------" ); System.out.println(details); System.out.println("--------------------------------------------------" ); break ; case '2' : System.out.print("本次收入金额:" ); int money1 = Utility.readNumber(); System.out.print("本次收入说明:" ); String info1 = Utility.readString(); balance += money1; details += "收入\t" + balance + "\t\t" + money1 + "\t\t" + info1 + "\n" ; System.out.println("---------------------登记完成---------------------" ); break ; case '3' : System.out.print("本次支出金额:" ); int money2 = Utility.readNumber(); if (balance > money2){ balance -= money2; }else { System.out.println("支出超出账户可用额度!!!" ); break ; } System.out.print("本次支出说明:" ); String info2 = Utility.readString(); details += "支出\t" + balance + "\t\t" + money2 + "\t\t" + info2 + "\n" ; System.out.println("---------------------登记完成---------------------" ); break ; case '4' : System.out.print("请确认是否退出(Y/N): " ); char exit = Utility.readConfirmSelection(); if (exit == 'Y' ) isFlag = false ; break ; } }while (isFlag); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 import java.util.Scanner;public class Utility { private static Scanner scanner = new Scanner (System.in); public static char readMenuSelection () { char c; for (; ; ) { String str = readKeyBoard(1 ); c = str.charAt(0 ); if (c != '1' && c != '2' && c != '3' && c != '4' ) { System.out.print("选择错误,请重新输入:" ); } else break ; } return c; } public static int readNumber () { int n; for (; ; ) { String str = readKeyBoard(4 ); try { n = Integer.parseInt(str); break ; } catch (NumberFormatException e) { System.out.print("数字输入错误,请重新输入:" ); } } return n; } public static String readString () { String str = readKeyBoard(8 ); return str; } public static char readConfirmSelection () { char c; for (; ; ) { String str = readKeyBoard(1 ).toUpperCase(); c = str.charAt(0 ); if (c == 'Y' || c == 'N' ) { break ; } else { System.out.print("选择错误,请重新输入:" ); } } return c; } private static String readKeyBoard (int limit) { String line = "" ; while (scanner.hasNext()) { line = scanner.nextLine(); if (line.length() < 1 || line.length() > limit) { System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:" ); continue ; } break ; } return line; } }