BJP3 Self-Check 5.23: robustInput Scanner

来源:互联网 发布:最大子数组 动态规划 编辑:程序博客网 时间:2024/06/05 23:42

网址:点击打开链接

要求:


代码:

Scanner console = new Scanner(System.in);int age = 0;double gpa = 0.0;System.out.print("Type your age: ");while(!console.hasNextInt()) {    try {        age = console.nextInt();    }catch(Exception e) {        System.out.print("Type your age: ");    }  }age = console.nextInt();System.out.print("Type your GPA: ");while(!console.hasNextDouble()) {    try {        gpa = console.nextDouble();    }catch(Exception e) {        System.out.print("Type your GPA: ");    }  }gpa = console.nextDouble();System.out.println("age = " + age + ", GPA = " + gpa);
结果:



0 0