/** * Displays the corresponding week according to the number entered by the user. * * Idea: * 1. The ontology conforms to the thought of looking up the table, can set a method, let the user pass an integer value, then check the corresponding day of the week in the table to be able. * */
Public classWhatday { Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); System.out.println ("Please enter an integer value, and I'll tell you what day of the week you entered?"); /*Here you can use int num = Scan.nextint () and then pass in the value of the method, which can be judged in the method. Since I learned the string class, I want to try it with string. A string is involved in the conversion type, and the string is converted to an Integer using its static method: Integer.prase (str); */String Userscan= Scan.next ();//Note: At this point the user is free to enter, whether digital or Chinese /*add difficulty to determine whether the user entered is not an integer, the user entered is not the shape of the value prompt him to enter again.*/ /*idea: 1. Determine whether user input is required or string type, using regular expressions. 2.String Userregex = "[1-7]"; Since it is judgment week, then there are only 1 possible, that is, the value between 1~7*/String Userregex= "([0][1-7]) | [1-7] ";//only receive 01, 02 or Format while(true){ if(Userscan.matches (Userregex)) {whatday (Userscan); Break; }Else{System.out.println ("Enter an illegal character, please enter it again"); Userscan=Scan.next (); } } } /*** User input value, the computer returns the days of the man tips *@paramUserscan User-entered string*/ Public Static voidWhatday (String userscan) {//first, the string is passed in, and it needs to be type-converted intnum =Integer.parseint (Userscan); //Create a table with subscript corresponding to the int number enteredString[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; //collections can also be implementedList<string> daylist =NewArraylist<string>(); Daylist.add ("Monday"); Daylist.add ("Tuesday"); Daylist.add ("Wednesday"); Daylist.add ("Thursday"); Daylist.add ("Friday"); Daylist.add ("Saturday"); Daylist.add ("Week Seven"); //Output ResultsSYSTEM.OUT.PRINTLN ("After calculation, you entered:" +daylist.get (num-1)); } /*Output: Please enter an integer value, and I'll tell you what the day of the week is entered. 06 After calculation, you entered: Saturday Please enter an integer value, and I'll tell you what day of the week you entered? 7 after calculation, you entered: Sunday*/}View Code/** * Displays the corresponding week according to the number entered by the user. * 2. Is it easier to use a map to do something? * Key is 1~7 value for week 1~7 * Enter key value, return value * Use map to Achieve * Conclusion: Map can be achieved, but it does not reflect the characteristics of the map. * */
/*** According to the number entered by the user, the corresponding week is displayed. * 2. Using map to make it seem easier? * Key is 1~7 value for week 1~7 * Enter key value, return value * Use map to Achieve * conclusion : Map can be implemented, but it does not reflect the characteristics of the map. * */ Public classWhatdaymap { Public Static voidMain (string[] args) {intnum = 7;//assume that the user entered the number 7. (omitted)Whatday (num);//a method of judging the day of the week } Public Static voidWhatday (intnum) {//because it is orderly, so with treemap, but feel map with order, can not play out the characteristics. So this problem is still useful with arrays or sets.Map<integer,string> Daymap =NewTreemap<integer,string>(); Daymap.put (1, "Monday"); Daymap.put (2, "Tuesday"); Daymap.put (3, "Wednesday"); Daymap.put (4, "Thursday"); Daymap.put (5, "Friday"); Daymap.put (6, "Saturday"); Daymap.put (7, "Sunday"); SYSTEM.OUT.PRINTLN (Daymap.get (num)); }}View Code
The
Displays the corresponding week based on the number entered by the user.