B站韩顺平老师讲解多用户通信时用到的Utility类

public class Utility {
    private static Scanner scanner;

    static {
        scanner = new Scanner(System.in);
    }

    public Utility() {

    }

    public static char readMenuSelection() {
        while (true) {
            String str = readKeyBoard(1, false);
            char c = str.charAt(0);
            if (c == 1 || c == 2 || c == 3 || c == 4 || c == 5) {
                return c;
            }

            System.out.print("选择错误,请重新输入:");
        }
    }

    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(){
        while (true){
            String str=readKeyBoard(2, false);
            try {
                int n=Integer.parseInt(str);
                return n;
            }catch (NumberFormatException var3){
                System.out.println("数字输入错误,请重新输入:");
            }
        }
    }

    public static int readInt(int defaultValue) {
        while(true) {
            String str = readKeyBoard(2, true);
            if (str.equals("")) {
                return defaultValue;
            }

            try {
                int n = Integer.parseInt(str);
                return n;
            } catch (NumberFormatException var4) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
    }

    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(){
        while (true){
            String str=readKeyBoard(1,false).toUpperCase();
            char c=str.charAt(0);
            if(c=='Y'||c=='N'){
                return c;
            }
            System.out.print("选择错误,请重新输入:");
        }
    }

    private static String readKeyBoard(int limit, boolean blankReturn){
        String line="";

        while (scanner.hasNextLine()){
            line=scanner.nextLine();
            if(line.length()==0){
                if(blankReturn){
                    return line;
                }
            }else {
                if(line.length()>=1 && line.length()<=limit){
                    break;
                }
                System.out.println("输入长度(不大于" + limit + ")错误,请重新输入:");
            }
        }
        return line;
    }
}

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: 韩顺平utility工具提供了许多实用的工具方法,可以帮助Java程序员更加高效地完成代码编写工作。该工具包含了各种常用的工具方法,如字符串处理、数字转换、日期处理等等。其中,字符串处理方法包括字符串比较、截取、替换、分割等等,可以帮助程序员更方便地对字符串进行处理;数字转换方法可以将不同进制的数字相互转换,也可以将数字转换为字符串;日期处理方法则可以方便地完成日期的格式化、加减、计算等操作。 除了这些基本的工具方法之外,韩顺平utility工具还提供了一些高级的工具方法,如加密解密、文件处理、网络访问等等。其中,加密解密方法支持对常见的加密算法进行加密和解密操作,满足一定的安全要求;文件处理方法可以方便地对文件进行读写、复制、删除等操作;网络访问方法则可以通过HTTP和TCP协议进行网络访问操作,支持数据的收发、文件上传和下载等操作。 总的来说,韩顺平utility工具提供了一系列实用的工具方法,可以帮助Java程序员更加高效地完成日常的开发工作。通过使用该工具,可以减少开发难度和提高开发效率,从而更加专注于业务逻辑的实现和优化。 ### 回答2: 韩顺平utility工具是指韩顺平教育公司开发的一种软件工具集合,旨在为Java程序员提供方便实用的实用工具库。该工具库包含了大量的Java常用工具,如字符串工具、集合工具、文件操作工具等,可以大大提升程序员编程效率,减少代码编写量,提高代码可读性和可维护性。 该工具库还提供了一些实用的工具方法,如日期格式化、正则表达式匹配、加密解密、线程池操作等,能够满足程序员在日常开发中的多种需求,节约研发间。同,该工具库还提供了详细的API文档和程序示例,方便程序员进行学习和使用。 韩顺平utility工具库的设计理念是简单易用、高效实用、稳定可靠,已经被广大Java程序员广泛应用于各个领域的项目中,成为了Java开发的常用工具之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值