关于java:如何获取一个月的输入以匹配案例#

How to get the input of a month to match with a case #

我不知道如何得到一个月的输入来匹配一个案例。我有点明白该怎么做,但不知道还要在代码中添加什么才能使代码正常工作。

这是任务:

Write a program that prompts the user for a month name and then uses a switch to display the name of the season. Assume spring months are March, April and May; summer is June, July, August; autumn is September, October, November; winter is December, January, February. You must use a switch. For full points your switch code should demonstrate an economy of statements and it should respond with an error message for bad inputs

这就是我目前为止所拥有的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
public class Assignment4 {
    private static Scanner input;
    public static void main(String[] args) {
        input = new Scanner(System.in);
        System.out.println("Enter the name of a month");
        int month = input.nextInt();
        switch (month) {
          case 12: season ="winter"; break;
          case 1: season ="winter"; break;
          case 2: season ="winter"; break;
          case 3: season ="spring"; break;
          case 4: season ="spring"; break;
          case 5: season ="spring"; break;
          case 6: season ="summer"; break;
          case 7: season ="summer"; break;
          case 8: season ="summer"; break;
          case 9: season ="fall"; break;
          case 10: season ="fall"; break;
          case 11: season ="fall"; break;
          default: System.out.println("Error: invalid month.");
}
        System.out.println("The season of the month" + month +"is" + season +".");
}}


当你快到了的时候,有两个让你更进一步的提示:

Write a program that prompts the user for a month name and then uses a switch to display the

因此使用:

1
String month = scanner.next();

请求一个字符串;然后切换:

1
case"december" : ...

就是这样!

此外,您可能需要在传入字符串上调用trim();您还需要使用toLowerCase()来确保不会遇到与"十二月"不同的"十二月"。


对于代码语句的经济性,可以同时使用多个case。

取输入

1
String month = input.nextLine().trim().toLowerCase();

开关箱

1
2
3
4
5
6
7
8
9
10
11
switch (month) {
        case"march":
        case"april":
        case"may":
            season ="spring";
            break;
        case"june":
        case"july":
        case"august":
            season ="summer";
            break;

按照@ghostcat的建议添加了trim和小写


你可以这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Scanner input = new Scanner(System.in);
System.out.println("Enter the month:");
try{ // wrap with try-catch block in case user entered invalid input (not a number)
    int month = Integer.parseInt(input.nextLine()); // it's better to read the entire line then parse it

    // use the ternary condition and one String variable
    String result = (month==1||month==2||month==12)  ?"The season of the month" + month +" is Winter."  :
                    (month==3||month==4||month==5)   ?"The season of the month" + month +" is Spring."  :
                    (month==6||month==7||month==8)   ?"The season of the month" + month +" is Summber." :
                    (month==9||month==10||month==11) ?"The season of the month" + month +" is Fall."    :"Error: invalid month.";

    System.out.println(result); // print out the result

   }catch(NumberFormatException e){ // if your program reaches this point that means it's not a number
        System.out.println("Error: invalid Input, Please use number only.");
   }

试验

1
2
3
4
5
Enter the month:
Text  ->  Error: invalid Input, Please use number only.
1     ->  The season of the month 1 is Winter.
5     ->  The season of the month 5 is Spring.
13    ->  Error: invalid month.

更新:

如果要求您使用switch作为强制要求,您可以这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Scanner input = new Scanner(System.in);
System.out.println("Enter the name of a month:");

String month = input.nextLine().trim(); // read the entire line and remove leading spaces by using trim() method


String result;
switch(month.toLowerCase()){
    case"january": case"february": case"december":
        result ="The season of the month" + month +" is Winter.";
    break;

    case"march": case"april": case"may":
        result ="The season of the month" + month +" is Spring.";
    break;

    case"jun": case"july": case"august":
        result ="The season of the month" + month +" is Summber.";
    break;

    case"september": case"october": case"november":
        result ="The season of the month" + month +" is Fall.";
    break;

    default:
        result ="Error: invalid month.";
}


 System.out.println(result); // print out the result

试验

1
2
3
4
Enter the name of a month:
January     ->  The season of the month January is Winter.
APRIL       ->  The season of the month APRIL is Spring.    
Not a month ->  Error: invalid month.

**你还没有在课堂上宣布季节,试试这个吧**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Assignment4 {
private static Scanner input;
public static void main(String[] args) {
    input = new Scanner(System.in);
    String season=null;
    System.out.println("Enter the name of a month");
    int month = input.nextInt();
    switch (month) {
      case 12: season ="winter"; break;
      case 1: season ="winter"; break;
      case 2: season ="winter"; break;
      case 3: season ="spring"; break;
      case 4: season ="spring"; break;
      case 5: season ="spring"; break;
      case 6: season ="summer"; break;
      case 7: season ="summer"; break;
      case 8: season ="summer"; break;
      case 9: season ="fall"; break;
      case 10: season ="fall"; break;
      case 11: season ="fall"; break;
      default: System.out.println("Error: invalid month.");

}  

         System.out.println("The season of the month" + month +"is" + season +".");
    }}

这个的输出是enter image description here


您应该能够打开字符串,但是如果您想保留这段代码,您需要一个函数,它将接受一个字符串,比如"june",并返回6,或者"janny"返回12。