本帖最后由 布鲁Go 于 2013-12-30 23:01 编辑
。像这样数量不多的,一般不这么猛用 if 吧 ,应该换成switch或者查表法,就好像上面那个那样,至于出错,是因为没有try catch.我试着写了简练些的。。- import java.util.Scanner;
- class Hello{
- public static void main(String[] args) throws Exception
- {
- String[] week = {"" ,"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
- System.out.println("请输入数字1~7:");
- Scanner sc = new Scanner(System.in);
- int num;
- try {
- num = sc.nextInt();
- System.out.println(week[num]);
- } catch (Exception e) {
- System.out.println("请按提示输入,谢谢");
- }
- }
- }
复制代码 |