题目:请输入星期几的第一个字母来判断一下是星期几
如果第一个字母一样,则继续 判断第二个字母。
import java.util.*; public class Test{ String inpStr="";//输入的字符串 String[] weeks = {"Monday","Tuesday","Wednesday","Thursday","Friday","Satday","Sunday"}; //判断第一个字母存在?,不纯在则置inpStr为空 public boolean isFirst(char c) { switch(c){ case 'M': case 'T': case 'W': case 'F': case 'S':return true; } inpStr=""; System.out.println("Msg: first Character is no allow!inpStr aleardy clear....."); return false; } //判断可否得到唯一一个单词 public boolean canFind(String str){ int flag=0; for(int i=0;i<weeks.length;i++){ if(weeks[i].startsWith(str)) flag++; } return flag==1; } //取出那一天 public String getDay(String str){ String result=""; for(int i=0;i<weeks.length;i++){ if(weeks[i].startsWith(str)) result=weeks[i]; } return result; } public static void main(String args[]){ Test t = new Test(); String c; Scanner scan = new Scanner(System.in); //输入 do{ System.out.println("input"); c=scan.nextLine(); t.inpStr+=c; }while(t.isFirst(t.inpStr.charAt(0))!=true||t.canFind(t.inpStr)!=true); System.out.println(t.getDay(t.inpStr)); } } /*----- 运行输出 ---- C:\>java Test input y Msg: first Character is no allow!inpStr aleardy clear..... input M Monday C:\>java Test input T input u Tuesday C:\>java Test input S input u Sunday */