SoFunction
Updated on 2025-03-08

Clever talk about the basic usage of Scanner

Requirements: Enter a month on the keyboard and output the corresponding season of that month.

There are four seasons a year

3,4,5   Spring

6,7,8   Summer

9,10,11 Fall

12,1,2 Winter

analyze:

A: One month after keyboard input, use Scanner to implement it

B: Determine what month it is, and the corresponding season according to the month output

public class Test2 {
  public static void main(String[] args) {
    // Keyboard input for one month, use Scanner to implement it    Scanner sc = new Scanner();

    // Receive data    ("Please enter a month(1-12):");
    int month = ();
switch(month) {
    case 1:
    case 2:
    case 12:
      ("winter");
      break;
    case 3:
    case 4:
    case 5:
      ("spring");
      break;
    case 6:
    case 7:
    case 8:
      ("summer");
      break;
    case 9:
    case 10:
    case 11:
      ("autumn");
      break;
    default:
        ("The month you entered is incorrect");
        break;
    }
  }
}

Of course there are many other methods, here only one is demonstrated

The basic usage of Scanner in the above article is all the content I share with you. I hope you can give you a reference and I hope you support me more.