2015-04-23 80 views
2

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

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

回答

9

你需要找到匹配的情况下,将只执行所有的情况下,直到它找到折断或结束而你的情况是2010后其他各种情况后添加break声明。

+0

如果我希望将所选年份和每个案例一直列入第一个案例(2000年),我该怎么办? – Clarisa

+2

在这种情况下,您可以在2010年开始您的案例,并在2000年结束。 –

3

您的代码应该是这样的:

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

通知breakcase后。

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

这样,你会在进入今年的switch声明你提供,然后回落,一路过关斩将到最后的情况下,。这样,您的eventString始终包含2010年的值。为防止出现这种情况,只需在每个case中添加一条break语句。

0

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

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

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

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

所以,如果你想获得的是发生在这一年的所有太空探索的事实和以后,你可以省略休息时间。