public class Demo{
    public static void main(String[] args) {


        Demo d = new Demo();

        d.in();

    }

    public void in(){
        InputStreamReader in = null;

        in = new InputStreamReader(System.in);
        char[] c = new char[10];

        int i = -1;
        try {
            if ((i = in.read(c)) < 10){
                //输出输入的字符串
                System.out.println(new String(c,0,i));

            }else{
               throw new  MyStringException();
            }
        } catch (MyStringException e){
            System.out.println("==============================");
            System.out.println("请重新输入:");
            in();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class MyStringException extends Exception{
    public MyStringException(){
        System.out.println("错误:长度不足!");
    }
}
Last modification:May 5th, 2020 at 10:47 pm
如果觉得我的文章对你有用,请随意赞赏