【问题标题】:Trouble with switch statements?switch 语句有问题?
【发布时间】:2015-04-23 05:49:22
【问题描述】:

我只是想写一个程序,在 2000 年到 2010 年之间随机生成一个年份,然后读取那一年发生的太空探索事实。 这是我编写的代码,但是当我运行它时,无论生成哪一年,它都只会打印最后一个案例(2010 年)。我该如何解决这个问题?

import java.util.Random;

public class SpaceExploration {
public static void main(String[] args) {

int year =(int)(Math.random()*11) + 2000;
String eventString = "";

switch (year) {
    case 2000:  eventString = "2000: First spacecraft orbits an asteroid";
    case 2001:  eventString = "2001: First spacecraft lands on asteroid";
    case 2002:  eventString = "2002: N/A";
    case 2003:  eventString = "2003: Largest infrared telescope released";
    case 2004:  eventString = "2004: N/A";
    case 2005:  eventString = "2005: Spacecraft collies with comet";
    case 2006:  eventString = "2006: Spacecraft returns with collections from a comet";
    case 2007:  eventString = "2007: N/A";
    case 2008:  eventString = "2008: Kepler launched to study deep space";
    case 2009:  eventString = "2009: N/A";
    case 2010:  eventString = "2010: SpaceX sucessfully sends spacecraft to orbit and back";
    }
    System.out.println(eventString);
}
}

【问题讨论】:

    标签: java string random switch-statement case


    【解决方案1】:

    您需要在每个 case else 之后添加 break 语句,在找到匹配的 case 之后,它将只执行所有 case,直到找到 break 或在您的 case 中为 2010 的结尾。

    【讨论】:

    • 如果我希望它发布选择的年份和每个案例一直到第一个案例(2000 年),我会怎么做?
    • 在这种情况下,您可以从 2010 年开始并以 2000 年结束。
    【解决方案2】:

    您的代码应如下所示:

    switch (year) {
        case 2000:  
           eventString = "2000: First spacecraft orbits an asteroid";
           break;
        case 2001:  
           eventString = "2001: First spacecraft lands on asteroid";
           break;
       ...
    

    注意每个case 后面的break

    【讨论】:

      【解决方案3】:

      其他答案都是正确的,你必须在每个案例之后引入休息。 一般来说,您还应该添加 case default case 并添加一些控制台输出,以确保您的软件按预期工作。

      另一种解决方案是使用 HashMap:

      Map<Integer, String> map = new HashMap<>();
      map.put(2010, 2010: SpaceX sucessfully sends spacecraft to orbit and back);
      ...
      String eventString = map.get(year);
      

      【讨论】:

        【解决方案4】:

        像这样,您将在您提供的那一年输入switch 语句然后一直到最后一个案例。这样,您的 eventString 始终包含 2010 年的值。为防止这种情况,只需在每个 case 中添加一个 break 语句。

        【讨论】:

          【解决方案5】:

          Break 是用于从循环(while、do-while 和 for)和 switch 语句中跳转的关键字,当某些条件匹配并且您想从循环或 switch 中跳出来时,应该使用 break。

              while(true){
                  if(true) {
                      break ; // when you want to get out from the loop 
                  }
              }
          

          对于 switch 语句,您应该使用 break 语句来退出循环。

          【讨论】:

            【解决方案6】:

            您所经历的是switch 语句的一个特性,称为“fall through”。 尽管通常使用 break 变体(在大多数情况下都有意义),但仍有一些应用程序要按定义的顺序执行,在某些情况下会以瀑布式的方式出现。看看下面的应用(link):

            另一个有趣的地方是break语句。每个 break 语句都会终止封闭的 switch 语句。控制流继续 switch 块之后的第一条语句。 break 语句是必要的,因为没有它们,switch 块中的语句就会失败:匹配 case 标签之后的所有语句都按顺序执行,无论后续 case 标签的表达式如何,直到遇到 break 语句。程序 SwitchDemoFallThrough 显示了 switch 块中的语句。程序显示整数月份对应的月份和该年之后的月份:

            public class SwitchDemoFallThrough {
            
                public static void main(String[] args) {
                    java.util.ArrayList<String> futureMonths =
                        new java.util.ArrayList<String>();
            
                    int month = 8;
            
                    switch (month) {
                        case 1:  futureMonths.add("January");
                        case 2:  futureMonths.add("February");
                        case 3:  futureMonths.add("March");
                        case 4:  futureMonths.add("April");
                        case 5:  futureMonths.add("May");
                        case 6:  futureMonths.add("June");
                        case 7:  futureMonths.add("July");
                        case 8:  futureMonths.add("August");
                        case 9:  futureMonths.add("September");
                        case 10: futureMonths.add("October");
                        case 11: futureMonths.add("November");
                        case 12: futureMonths.add("December");
                                 break;
                        default: break;
                    }
            
                    if (futureMonths.isEmpty()) {
                        System.out.println("Invalid month number");
                    } else {
                        for (String monthName : futureMonths) {
                           System.out.println(monthName);
                        }
                    }
                }
            }

            因此,如果您想获取当年及以后发生的所有太空探索事实,您可以省略休息时间。

            【讨论】:

              猜你喜欢
              • 2010-12-20
              • 2018-10-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多