ADD file via upload

main
pc9pizjb6 4 months ago
parent 2824969591
commit 69101bc99a

@ -0,0 +1,108 @@
package com.studentmanagement.util;
import java.util.Scanner;
/**
*
*/
public class InputUtil {
private static final Scanner scanner = new Scanner(System.in);
/**
*
* @param prompt
* @return
*/
public static int getIntInput(String prompt) {
while (true) {
System.out.print(prompt);
try {
return Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.err.println("请输入有效的整数!");
}
}
}
/**
*
* @param prompt
* @param min
* @param max
* @return
*/
public static int getIntInput(String prompt, int min, int max) {
while (true) {
int value = getIntInput(prompt);
if (value >= min && value <= max) {
return value;
}
System.err.println("请输入" + min + "到" + max + "之间的整数!");
}
}
/**
*
* @param prompt
* @return
*/
public static String getStringInput(String prompt) {
System.out.print(prompt);
return scanner.nextLine().trim();
}
/**
*
* @param prompt
* @return
*/
public static String getNonEmptyStringInput(String prompt) {
while (true) {
String input = getStringInput(prompt);
if (!input.isEmpty()) {
return input;
}
System.err.println("输入不能为空!");
}
}
/**
*
* @param prompt
* @return
*/
public static double getDoubleInput(String prompt) {
while (true) {
System.out.print(prompt);
try {
return Double.parseDouble(scanner.nextLine());
} catch (NumberFormatException e) {
System.err.println("请输入有效的数字!");
}
}
}
/**
*
* @param prompt
* @param min
* @param max
* @return
*/
public static double getDoubleInput(String prompt, double min, double max) {
while (true) {
double value = getDoubleInput(prompt);
if (value >= min && value <= max) {
return value;
}
System.err.println("请输入" + min + "到" + max + "之间的数字!");
}
}
/**
*
*/
public static void close() {
scanner.close();
}
}
Loading…
Cancel
Save