日期:2014-05-20  浏览次数:20695 次

基础作业求解
从键盘输入以为整数,当输入1-7时,显示下面对应的英文星期名称的缩写。1:MON 2:TUE 3:WED 4:THU 5:FRI 6:SAT 7:SUN
输入其他数字时提示用户重新输入,输入数字0时程序结束。
图示:
**************************************
请输入数字1-7(输入0结束):2
今天是TUE
请输入数字1-7(输入0结束):5
今天是FRI
请输入数字1-7(输入0结束):0
程序结束!


------解决方案--------------------
Java code
    public static void main(final String[] args) {
        final String[] weekdays ={"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入数字1-7(输入0结束):");
            int x = scanner.nextInt();
            if (x == 0) break;
            if (x > 7 || x < 0) continue;
            System.out.printf("今天是 %s%n", weekdays[x-1]);
        }
    }

------解决方案--------------------
Java code



public class TestOutput{
    public static void main(String[] args) {
    String [] days = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
    Scanner scanner = new Scanner(System.in);
    System.out.print("请输入数字1-7(输入0结束)");
    while (scanner.hasNext()) {
        try {
        
        int temp = scanner.nextInt();
        if (temp == 0)
            break;
        if(temp<0||temp>7)
            throw new Exception("输入错误");
        
        
        System.out.println("今天是"+days[temp-1]);
        System.out.print("请输入数字1-7(输入0结束)");
        } catch (Exception e) {
        try {
            throw new Exception("输入错误");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        }
    }
    System.out.print("程序结束!");
    }
}