切换语句有问题?

时间:2015-04-23 05:49:23

标签: java string random switch-statement case

我只是想编写一个在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);
}
}

6 个答案:

答案 0 :(得分:9)

在找到匹配的大小写之后,你需要在每个案例之后添加break语句,它只会执行所有案例,直到它找到中断或者你的情况为2010的结尾。

答案 1 :(得分:3)

您的代码应如下所示:

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

在每break之后注意case

答案 2 :(得分:2)

其他答案都是正确的,你必须在每个案例后介绍休息。 一般情况下,您还应添加案例default案例并添加一些控制台输出,以确保您的软件按预期工作。

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

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

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

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

对于switch语句,你应该使用break语句从循环中退出。

答案 5 :(得分:0)

您所体验的是switch语句的一项功能,称为“通过”。 虽然通常使用中断变量(在大多数情况下这是有意义的),但是有一些应用程序以定义的顺序执行,在某些情况下以水上方式落下。查看下面的应用程序(link):

  

另一个兴趣点是break语句。每个break语句都会终止封闭的switch语句。控制流继续切换块后面的第一个语句。 break语句是必需的,因为没有它们,switch块中的语句都会失败:匹配的case标签之后的所有语句都按顺序执行,而不管后续case标签的表达式,直到遇到break语句。程序SwitchDemoFallThrough显示掉落的交换机块中的语句。该程序显示对应于整数月份的月份以及该年度中的月份:

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

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