工具类-获取控制台输入

[复制链接]
发表于 2023-2-18 11:38:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. /**
  2. * 工具类的作用
  3. * 处理各种情况下用户的输入,并且能够按照程序员的要求,得到用户的控制台输入。
  4. */
  5. public class Utility {
  6.     //静态属性
  7.     private static Scanner scanner = new Scanner(System.in);
  8.     /**
  9.      * 功能:读取键盘输入的一个菜单选项,值:1-5的范围
  10.      *
  11.      * @return 1-5
  12.      */
  13.     public static char readMenuSelection() {
  14.         char c;
  15.         while (true) {
  16.             String str = readKeyBoard(1, false);
  17.             c = str.charAt(0);//将字符串转换为字符char
  18.             if (c != '1' && c != '2'
  19.                     && c != '3' && c != '4' && c != '5') {
  20.                 System.out.println("选择错误,请重新输入!");
  21.             } else break;
  22.         }
  23.         return c;
  24.     }
  25.     /**
  26.      * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值
  27.      *
  28.      * @param defaultValue 指定的默认字符串
  29.      * @return 默认值或输入的字符
  30.      */
  31.     public static char readChar(char defaultValue) {
  32.         String str = readKeyBoard(1, true);//要么是空串,要么是输入的字符串
  33.         return (str.length() == 0 ? defaultValue : str.charAt(0));
  34.     }
  35.     public static char readChar() {
  36.         String str = readKeyBoard(1, false);//要么是空串,要么是输入的字符串
  37.         return str.charAt(0);
  38.     }
  39.     /**
  40.      * 功能:读取键盘输入的整型,长度小于2位
  41.      *
  42.      * @parameter 整数
  43.      */
  44.     public static int readInt(int defaultValue) {
  45.         int n;
  46.         while (true) {
  47.             String str = readKeyBoard(10, true);
  48.             if (str.equals("")) {
  49.                 return defaultValue;
  50.             }
  51.             //异常处理
  52.             try {
  53.                 n = Integer.parseInt(str);
  54.                 break;
  55.             } catch (NumberFormatException e) {
  56.                 System.out.println(e);
  57.             }
  58.         }
  59.         return n;
  60.     }
  61.     /**
  62.      * 功能:读取键盘输入的指定长度的字符串
  63.      *
  64.      * @return 指定长度的字符串
  65.      * @parameter limit 限制的长度
  66.      */
  67.     public static String readString(int limit) {
  68.         return readKeyBoard(limit, false);
  69.     }
  70.     /**
  71.      * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认的字符串
  72.      *
  73.      * @param limit        限制的长度
  74.      * @param defaultValue 指定的默认值
  75.      * @return 指定长度的字符串
  76.      */
  77.     public static String readString(int limit, String defaultValue) {
  78.         String str = readKeyBoard(limit, true);
  79.         return str.equals("") ? defaultValue : str;
  80.     }
  81.     /**
  82.      * 功能:从键盘读取输入的选项,Y/N
  83.      * 将小的功能封装到一个方法中
  84.      *
  85.      * @return Y/N
  86.      */
  87.     public static char readConfirmSelection() {
  88.         System.out.println("请输入你的选择(Y/N),请小心选择:");
  89.         char c;
  90.         while (true) {//无限循环
  91.             //在这里,将接受到字符,转成了大写字母
  92.             //y=>Y n=>N
  93.             String str = readKeyBoard(1, false).toUpperCase();
  94.             c = str.charAt(0);
  95.             if (c == 'Y' || c == 'N') {
  96.                 break;
  97.             } else {
  98.                 System.out.println("选择错误,请重新输入:");
  99.             }
  100.         }
  101.         return c;
  102.     }
  103.     /**
  104.      * 功能:从键盘读取字符串
  105.      *
  106.      * @param flag  判断是否严格
  107.      * @param limit 是否可以为空字符串
  108.      * @return str 符合要求的字符串
  109.      */
  110.     public static String readKeyBoard(int limit, boolean flag) {
  111.         String str;
  112.         do {
  113.             str = scanner.nextLine();
  114.             if (str.length() == 0) {
  115.                 if (flag == false) {
  116.                     while (true) {
  117.                         System.out.println("请输入:");
  118.                         str = scanner.nextLine();
  119.                         if (str.length() != 0) {
  120.                             break;
  121.                         }
  122.                     }
  123.                 } else
  124.                     return "";
  125.             }
  126.             if (str.length() > limit){
  127.                 System.out.print("请输入长度不大于" + limit +"的字符串:");
  128.             }
  129.         }while (str.length() > limit);
  130.     return str;
  131.     }
  132. }
复制代码
 
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表