| 写了一段代码给你,不知道对你是否有帮助 
 复制代码package com.heima;
import java.util.Scanner;
/**
 * 输入数字1~7,输出对应的星期
 * 非法输入则要求再次输入,直到正确为止
 */
public class Demo {
        public static void main(String[] args) {
                String[] week = new String[]{"星期一","星期二","星期三","星期四",
                                "星期五","星期六","星期天"};
                System.out.println("请输入数字1~7:");
                Scanner sc = new Scanner(System.in);
                String input = sc.nextLine();
                while(true){
                        if(input.length()!=1){//输入了多个字符
                                System.out.println("输入错误,你输入了多个字符,请输入数字1~7:");
                                input = sc.nextLine();
                                continue;
                        }
                        char ch = input.charAt(0);//得到输入的字符
                        if(ch<'1' || ch>'7'){
                                System.out.println("输入错误,请输入数字1~7:");
                                input = sc.nextLine();
                                continue;
                        }
                        //这的49先是减去ascii的值,然后,你知道,数组是从0开始的。
                        System.out.println("输入正确:"+week[ch-49]);
                        break;
                        
                }
        }
}
 |