Java input letter to determine the day of the week implementation code

  • 2020-06-15 08:26:31
  • OfStack

Please enter the first letter of the week to determine the day of the week.

Program analysis: use case statement is better, if the first letter 1, then judge the use of case statement or if statement to judge the second letter.

Program design:


import java.util.Scanner;
public class Ex26 {
 public static void main(String[] args){
 // Save the input of the user 2 A letter 
 char weekSecond;
 // will Scanner Class instantiation input Object to receive user input 
 Scanner input = new Scanner(System.in);
 // Start prompting and receive user console input 
 System.out.print(" Please enter the day of the week 1 Let me tell you what day of the week it is: ");
 String letter = input.next();
 // Determines whether the user console input string length is 1 A letter 
 if (letter.length() == 1){
  // Take the first use of 1 Three index bits of characters to achieve let Scanner receive char Type of input 
  char weekFirst = letter.charAt(0);
  switch (weekFirst){
 case 'm':
   // When typing lowercase letters, use switch Structural properties are executed under 1 A belt break The statement case Branch to implement the ability to ignore case-sensitive user console input 
  case 'M':
   System.out.println(" week 1(Monday)");
   break;
   case 't':
   // When typing lowercase letters, use switch Structural properties are executed under 1 A belt break The statement case Branch to implement the ability to ignore case-sensitive user console input 
  case 'T':
   System.out.print(" Due to the week 2(Tuesday) With a week 4(Thursday) In the letter T Start, so you need to enter the first 2 It takes six letters to make a correct judgment: ");
   letter = input.next();
   // Determines whether the user console input string length is 1 A letter 
   if (letter.length() == 1){
   // Take the first use of 1 Three index bits of characters to achieve let Scanner receive char Type of input 
   weekSecond = letter.charAt(0);
   // Use or ( || ) operator to ignore case-sensitive user console input 
   if (weekSecond == 'U' || weekSecond == 'u'){
    System.out.println(" week 2(Tuesday)");
    break;
   // Use or ( || ) operator to ignore case-sensitive user console input 
   } else if (weekSecond == 'H' || weekSecond == 'h'){
    System.out.println(" week 4(Thursday)");
    break;
   // Console error prompt 
   } else{
    System.out.println(" Incorrect input. Unrecognized day of the week 2 Four letters, program over! ");
    break;
   }
   } else {
   // Console error prompt 
   System.out.println(" Input error, input only 1 Four letters, program over! ");
   break;
   }
  case 'w':
   // When typing lowercase letters, use switch Structural properties are executed under 1 A belt break The statement case Branch to implement the ability to ignore case-sensitive user console input 
  case 'W':
   System.out.println(" week 3(Wednesday)");
   break;
  case 'f':
   // When typing lowercase letters, use switch Structural properties are executed under 1 A belt break The statement case Branch to implement the ability to ignore case-sensitive user console input 
  case 'F':
   System.out.println(" week 5(Friday)");
   break;
  case 's':
   // When typing lowercase letters, use switch Structural properties are executed under 1 A belt break The statement case Branch to implement the ability to ignore case-sensitive user console input 
  case 'S':
   System.out.print(" Due to the week 6(Saturday) And on Sunday (Sunday) In the letter S Start, so you need to enter the first 2 It takes six letters to make a correct judgment: ");
   letter = input.next();
   // Determines whether the user console input string length is 1 A letter 
   if (letter.length() == 1){
   // Take the first use of 1 Three index bits of characters to achieve let Scanner receive char Type of input 
   weekSecond = letter.charAt(0);
   // Use or ( || ) operator to ignore case-sensitive user console input 
   if (weekSecond == 'A' || weekSecond == 'a'){
    System.out.println(" week 6(Saturday)");
    break;
   // Use or ( || ) operator to ignore case-sensitive user console input 
   } else if (weekSecond == 'U' || weekSecond == 'u'){
    System.out.println(" Sunday (Sunday)");
    break;
   // Console error prompt 
   } else{
    System.out.println(" Incorrect input. Unrecognized day of the week 2 Four letters, program over! ");
    break;
   }
   } else{
   // Console error prompt 
   System.out.println(" Input error, input only 1 Four letters, program over! ");
   break;
   }
  default:
   // Console error prompt 
   System.out.println(" Incorrect input. Unrecognized day of the week 1 Four letters, program over! ");
   break;
  }
 } else{
  // Console error prompt 
  System.out.println(" Input error, input only 1 Four letters, program over! ");
 }
 }
}


Related articles: