Java编程题,刚刚学习编程,啥都不会,求大佬帮助

编写一个测试用户输入是否是一年中的12个月的程序。要求用户输入月份值,如果月份值在1~12之间,则正常输出用户输入的月份,否则抛出ArithmeticExceptoion异常。
并输出:
“捕获ArithmeticExceptoion异常”。

public static void main(String[] args) {

try {

int month = getInputMonth();

System.out.println("你输入的月份是" + month);

} catch (ArithmeticException ex) {

System.out.println("捕获ArithmeticExceptoion异常");

}

}

private static int getInputMonth() throws ArithmeticException {

Scanner scan = new Scanner(System.in);

if (scan.hasNextInt()) {

int month = scan.nextInt();

if (month >= 1 && month <= 12) {

return month;

}

}

throw new ArithmeticException();

}

关于JAVA有什么不懂的可以随时问我

温馨提示:内容为网友见解,仅供参考
第1个回答  2019-05-14
要拜师吗?
相似回答