加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

尚硅谷JavaSE进阶第9章Java异常处理项目3:开发团队调度系统5、把键盘输入工具类TSUtility添加到项目中

(2018-10-23 15:07:44)
标签:

it

java培训

linux

尚硅谷

大数据

分类: 大数据学科

5、把键盘输入工具类TSUtility添加到项目中

直接将提供的TSUtility.java文件拷贝到com.atguigu.util包中,发现需要修改包名。

第二发现出现乱码,因为提供的文件是UTF-8格式的,如果你默认的项目字符编码为GBK的话,就会乱码,解决办法可以用记事本打开TSUtility.java文件,把代码复制到eclipse中。或者修改项目的字符编码为UTF-8,不过设置项目字符编码最好在项目一开始。

package com.atguigu.util;

 

import java.util.*;

 

public class TSUtility {

    private static Scanner scanner = new Scanner(System.in);

 

public static char readMenuSelection() {

        char c;

        for (; ; ) {

            String str = readKeyBoard(1, false);

            c = str.charAt(0);

            if (c != '1' && c != '2' &&

                c != '3' && c != '4') {

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

            } else break;

        }

        return c;

    }

 

    public static void readReturn() {

        System.out.print("按回车键继续...");

        readKeyBoard(100, true);

    }

 

    public static int readInt() {

        int n;

        for (; ; ) {

            String str = readKeyBoard(2, false);

            try {

                n = Integer.parseInt(str);

                break;

            } catch (NumberFormatException e) {

                System.out.print("数字输入错误,请重新输入:");

            }

        }

        return n;

    }

 

    public static char readConfirmSelection() {

        char c;

        for (; ; ) {

            String str = readKeyBoard(1, false).toUpperCase();

            c = str.charAt(0);

            if (c == 'Y' || c == 'N') {

                break;

            } else {

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

            }

        }

        return c;

    }

 

    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 continue;

            }

 

            if (line.length() < 1 || line.length() > limit) {

                System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");

                continue;

            }

            break;

        }

 

        return line;

    }

}

 本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源,欢迎大家关注尚硅谷公众号(atguigu)了解更多。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有