有没有发现控制台老是有问题?我改了下代码。还有就是这个我改的代码为啥不打印输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public class TryDemoOne3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); try { System.out.println( "运算开始" ); System.out.println( "请输入第一个整数" ); int one = sc.nextInt(); System.out.println( "请输入第二个整数" ); Integer two = sc.nextInt(); if (two.equals( null )) { while (two.equals( null )) { //这里为什么无法输出下面这个提示信息? System.out.println( "请重新输入" ); two = sc.nextInt(); } } System.out.println( "one和two的商:" + one / two); } catch (java.util.InputMismatchException e) { System.out.println( "输入的类型不匹配,请输入整数" ); } catch (java.lang.ArithmeticException e) { System.out.println( "除数不能为零,请重新输入" ); } catch (Exception e) { System.out.println( "你咋不上天" ); } finally { System.out.println( "运算结束" ); sc.close(); System.exit( 1 ); } } } |
42
收起
正在回答
4回答
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import java.util.Scanner; public class TryDemoOne3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); try { System.out.println( "运算开始" ); System.out.println( "请输入第一个整数" ); int one = sc.nextInt(); System.out.println( "请输入第二个整数" ); String two = sc.nextLine(); Integer two1 = null ; two = sc.nextLine(); //two1=Integer.parseInt(two); if (two.equals( "" )) { while (two.equals( "" )) { //这里为什么无法输出下面这个提示信息? System.out.println( "请重新输入" ); two = sc.nextLine(); } } two1=Integer.parseInt(two); //将字符串转换为整型 System.out.println( "one和two的商:" + one / two1); } catch (java.util.InputMismatchException e) { System.out.println( "输入的类型不匹配,请输入整数" ); } catch (java.lang.ArithmeticException e) { System.out.println( "除数不能为零,请重新输入" ); } catch (Exception e) { System.out.println( "你咋不上天" ); } finally { System.out.println( "运算结束" ); sc.close(); System.exit( 1 ); } } } |
运行一下上面的代码,看看是不是你想要的效果。如果有问题可以继续提问,祝学习愉快!
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧