You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.1 KiB
81 lines
2.1 KiB
package com.studentmanagement.util;
|
|
|
|
import java.util.Scanner;
|
|
|
|
/**
|
|
* 输入工具类
|
|
* 提供用于获取用户输入的工具方法
|
|
*/
|
|
public class InputUtil {
|
|
private static Scanner scanner = new Scanner(System.in);
|
|
|
|
/**
|
|
* 获取字符串输入
|
|
* @param prompt 提示信息
|
|
* @return 用户输入的字符串
|
|
*/
|
|
public static String getStringInput(String prompt) {
|
|
System.out.print(prompt);
|
|
return scanner.nextLine();
|
|
}
|
|
|
|
/**
|
|
* 获取整数输入
|
|
* @param prompt 提示信息
|
|
* @return 用户输入的整数
|
|
*/
|
|
public static int getIntInput(String prompt) {
|
|
while (true) {
|
|
System.out.print(prompt);
|
|
String input = scanner.nextLine();
|
|
try {
|
|
return Integer.parseInt(input);
|
|
} catch (NumberFormatException e) {
|
|
System.out.println("请输入有效的整数");
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取浮点数输入
|
|
* @param prompt 提示信息
|
|
* @return 用户输入的浮点数
|
|
*/
|
|
public static double getDoubleInput(String prompt) {
|
|
while (true) {
|
|
System.out.print(prompt);
|
|
String input = scanner.nextLine();
|
|
try {
|
|
return Double.parseDouble(input);
|
|
} catch (NumberFormatException e) {
|
|
System.out.println("请输入有效的数字");
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取确认输入
|
|
* @param prompt 提示信息
|
|
* @return 是否确认
|
|
*/
|
|
public static boolean getConfirmation(String prompt) {
|
|
while (true) {
|
|
String input = getStringInput(prompt + " (y/n): ");
|
|
if (input.equalsIgnoreCase("y")) {
|
|
return true;
|
|
} else if (input.equalsIgnoreCase("n")) {
|
|
return false;
|
|
} else {
|
|
System.out.println("请输入 y 或 n");
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 等待用户按Enter键
|
|
*/
|
|
public static void waitForEnter() {
|
|
System.out.println("\n按Enter键继续...");
|
|
scanner.nextLine();
|
|
}
|
|
} |