我们如何使用字符串切换案例

时间:2013-10-04 09:33:00

标签: java

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。

8 个答案:

答案 0 :(得分:5)

问题是你正在开启String 数组而不是String ...

switch(args[0])

会工作 - 假设你使用 JDK7 ...并且有一个参数提供给你的程序 - 否则你会得到一个很好的ArrayOutOfBoundsException ......

答案 1 :(得分:0)

JDK版本7中引入了带有Switch

String。所以你需要升级到jdk7。

答案 2 :(得分: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");
    }
}

答案 3 :(得分:0)

您需要从参数中创建一个一个字符串,而不是全部。

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

实际上,如果你在解码几天,你应该把这个逻辑放在一个解析类中 - 并且可能不会将它们编码为dayN。考虑不区分大小写:

希望这只是你的测试用例..而不是真正的设计。

不区分大小写(强制为小写):

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

答案 4 :(得分:0)

在JDK 7发行版中,您可以在switch语句的表达式中使用String对象:

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语句将其表达式中的String对象与每个case标签关联的表达式进行比较,就像它使用String.equals方法一样;因此,switch语句中String对象的比较区分大小写。 Java编译器通常使用String对象生成比使用链接if-then-else语句的switch语句更高效的字节码。

了解更多信息,请点击here

答案 5 :(得分:0)

最近在Java SE 7中添加的switch语句中的字符串。

使用JDK 7,您可以在switch语句中传递string as expression。

答案 6 :(得分:0)

您也可以将switch与String一起使用,但是您尝试使用String数组。选择一个你想要的参数并将其用作开关中的String。

例如:

String yourChoice = args[0];

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

此外,您必须使用Java7或更新版本,并且必须确保args [0]不为null - 您必须在运行JAR时输入参数。

答案 7 :(得分:0)

搜索项目硬币,您将获得有关此内容的更多信息。或者你可以在这里转到:http://java.dzone.com/articles/java-7-%E2%80%93-project-coin-feature dzone,tutorial。

您可以从这里下载java 7:http://www.oracle.com/technetwork/java/javase/downloads/index.html

相关问题