2013-10-04 21 views
1
public class SwitchExampleString 
{ 
    public static void main(String args[]) 
    { 
     String choice; 
     switch(args) 
     { 
      case "day1" : 
       choice="Sunday"; 
       System.out.println(choice); 
       break; 
      case "day2" : 
       choice="Monday"; 
       System.out.println(choice); 
       break; 
      case "day3" : 
       choice="Tuesday"; 
       System.out.println(choice); 
       break; 
      case "day4" : 
       choice="Wednesday"; 
       System.out.println(choice); 
       break; 
      case "day5" : 
       choice="Thursday"; 
       System.out.println(choice); 
       break; 
      case "day6" : 
       choice="Friday"; 
       System.out.println(choice); 
       break; 
      case "day7" : 
       choice="Saturday"; 
       System.out.println(choice); 
       break; 
      default : 
       System.out.println("Wrong choice"); 
     } 
    } 
} 

任何人都可以幫助我,我想知道如何使用switch()中的字符串。以上顯示的是我迄今爲止所做的程序。但它顯示錯誤。我已經安裝的Java版本是jdk6。我們怎麼能使用字符串切換案例

+2

只需安裝JDK7 .. Java 6中不允許在開關的操作字符串,而是從Java 7,你可以做到這一點。 – rokonoid

+2

可能重複的[在Java語言中使用字符串切換語句](http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java) –

+2

您正在打開'String []'而不是' String'。 –

回答

5

的問題是你在一個字符串數組一個字符串轉換...

switch(args[0]) 

會工作 - 給你使用JDK7我,而提供給您的說法計劃 - 否則,你就會得到一個不錯ArrayOutOfBoundsException ...

0

SwitchString在JDK版本7中引入。所以你需要升級到jdk7。

+0

我可以知道downvote的原因嗎?歐普提到使用的jdk版本是6。 –

0

檢查choiceargs[0]

public class SwitchExampleString 
{ 

public static void main(String args[]) 

{ 
    String choice="day1"; 
    switch(choice) 
    { 
case "day1" : 
    choice="Sunday"; 
     System.out.println(choice); 
    break; 
case "day2" : 
    choice="Monday"; 
     System.out.println(choice); 
    break; 
case "day3" : 
    choice="Tuesday"; 
     System.out.println(choice); 
    break; 
case "day4" : 
    choice="Wednesday"; 
     System.out.println(choice); 
    break; 
case "day5" : 
    choice="Thursday"; 
     System.out.println(choice); 
    break; 
case "day6" : 
    choice="Friday"; 
     System.out.println(choice); 
    break; 
case "day7" : 
    choice="Saturday"; 
     System.out.println(choice); 
    break; 
    default : 
System.out.println("Wrong choice"); 
    } 
} 
0

你需要這樣做一個字符串來自參數,而不是全部。

switch (args[0]) { 
    case "day1" : 
     //... 

實際上,如果你解碼日子裏,你應該把這個邏輯在解析類 - 可能不會對其進行編碼爲dayN無論是。請考慮大小寫不敏感,以及:

希望這只是您的測試用例..不是真正的設計。

隨着不區分大小寫(強制爲小寫):

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { 
    String typeOfDay; 
    switch (dayOfWeekArg) { 
     case "Monday": 
      typeOfDay = "Start of work week"; 
      break; 
     case "Tuesday": 
     case "Wednesday": 
     case "Thursday": 
      typeOfDay = "Midweek"; 
      break; 
     case "Friday": 
      typeOfDay = "End of work week"; 
      break; 
     case "Saturday": 
     case "Sunday": 
      typeOfDay = "Weekend"; 
      break; 
     default: 
      throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg); 
    } 
    return typeOfDay; 
} 

開關:

switch (args[0].toLowerCase()) { 
    case "day1" : 
     //... 
0

在JDK 7版本中,可以在switch語句的表達式中使用的字符串對象語句將其表達式中的String對象與每個case標籤關聯的表達式進行比較,就好像它使用String.equals方法一樣;因此,switch語句中的String對象的比較區分大小寫。 Java編譯器通過使用String對象的switch語句比鏈式if-then-else語句生成通常更高效的字節碼。

的更多信息請點擊其中最近的Java SE 7

使用JDK 7中添加的功能開關語句here

0

字符串,可以把字符串作爲switch語句表達。

0

您也可以使用帶有字符串的開關,但是您正嘗試使用字符串數組。選擇一個你想要的參數,並將其用作交換機中的字符串。

例如:

String yourChoice = args[0]; 

switch(yourChoice) 
{ 
    case "something": 
     System.out.print("this"); 
    case "somethingElse": 
     System.out.print("that"); 
} 

而且你必須使用Java7或更新,您必須確保ARGS [0]不爲空 - 當你運行你的JAR,你必須輸入參數。

相關問題