从键盘输入以为整数,当输入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
程序结束!

解决方案 »

  1.   

        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]);
            }
        }
      

  2.   


    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("程序结束!");
        }
    }
      

  3.   

    public class TestWriter
    {
    public static void main(String[] args)
    {
    String[] weekdays = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
    System.out.print("请输入数字1-7(输入0结束):");
    Scanner input = new Scanner(System.in);
    int n;

    while ((n = input.nextInt()) != 0)
    {
    if (n < 0 || n > 7)
    {
    System.out.print("请输入数字1-7(输入0结束):");
    }
    else
    {
    System.out.println("今天是" + weekdays[n-1]);
    }

    System.out.print("请输入数字1-7(输入0结束):");
    }

    System.out.println("程序结束!");
    }
    }