【问题标题】:How to get the input of a month to match with a case #如何获取一个月的输入以匹配案例#
【发布时间】:2017-05-23 17:42:17
【问题描述】:

我不知道如何获取月份的输入以匹配 case #。我有点知道该怎么做,但不确定要在我的代码中添加什么才能使其正常工作。

这是作业:

编写一个程序,提示用户输入月份名称,然后使用开关显示季节名称。假设春季月份是三月、四月和五月;夏季是六月、七月、八月;秋天是九月、十月、十一月;冬天是十二月,一月,二月。您必须使用开关。对于完整点,您的开关代码应该证明语句的经济性,并且它应该以错误消息响应错误输入

这是我目前所拥有的:

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 + ".");
}}

【问题讨论】:

  • ...什么不起作用?请修正你的缩进。
  • Enter the name,但您正在检查月份数!
  • 为了更经济的语句,您可以对 case 语句使用 fallthrough - 尽管这在专业代码中不是一件好事(容易出错,因为您也可能忘记了 break 语句):@ 987654323@

标签: java switch-statement java.util.scanner


【解决方案1】:

你快到了,有两个提示可以让你走得更远:

编写一个程序,提示用户输入月份名称,然后使用开关显示

所以使用:

String month = scanner.next();

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

case "december" : ...

就是这样!

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

【讨论】:

  • 对不起,我还是新手,我该把 String、trim() 和 toLowerCase() 放在哪里
  • 在输入被收集到一个变量(在本例中为字符串月份)之后和 switch 语句之前。您基本上是将输入清理为标准化格式。所以很自然,您会希望在获得输入之后但在使用输入之前执行此操作。
  • @michael 您可以链接函数并执行String month = scanner.next().trim().toLowerCase();,也可以在String month = scanner.next(); 之后添加单独的语句,例如month = month.trim();
  • @michael 我故意省略了这些细节。这是你的作业。只是谷歌的话,例如“java string trim”。向其他人解释所有细节时,您不会学到很多东西。
【解决方案2】:

为了节省代码语句,您可以同时使用多个大小写。

接受输入

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

开关盒

switch (month) {
        case "march":
        case "april":
        case "may":
            season = "spring";
            break;
        case "june":
        case "july":
        case "august":
            season = "summer";
            break;

按照@GhostCat 的建议添加了修剪和小写

【讨论】:

  • 但这总是将月份打印为小写字母!
  • 我其实是建议让他自己发现的 ;-)
【解决方案3】:

您应该能够对字符串进行切换,但如果您想保留此代码,您需要一个函数,该函数将接受像“June”这样的字符串并返回 6 或“January”返回 12。

【讨论】:

    【解决方案4】:

    **你还没有在课堂上声明赛季试试这个它工作**

    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 + ".");
        }}
    

    为此

    【讨论】:

      【解决方案5】:

      你可以这样做:

      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.");
         }
      

      测试

      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 作为强制性要求,您可以这样做:

      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
      

      测试

      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.
      

      【讨论】:

        猜你喜欢
        • 2018-05-31
        • 2021-10-18
        • 1970-01-01
        • 2015-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-13
        • 2020-04-02
        相关资源
        最近更新 更多