2015-04-23 82 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); 
 
      } 
 
     } 
 
    } 
 
}

所以,如果你想獲得的是發生在這一年的所有太空探索的事實和以後,你可以省略休息時間。