“开关”是否已关闭? Java中的语句只适用于整数?

发布于 2024-10-20 03:13:42 字数 280 浏览 3 评论 0原文

可能的重复:
Java 中带有字符串的 Switch 语句?

仅 Java 中的“switch”语句使用整数?

我不能写这样的东西吗?

开关(字符串) 案例“你好”:...

谢谢

Possible Duplicate:
Switch Statement With Strings in Java?

Does the "switch" statement in Java only work with integers ?

Can't I write something like this instead ?

switch(string)
case "hello": ...

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

氛圍 2024-10-27 03:13:42

此答案仅对 Java 6 或更早版本有效! Java 7 中添加了字符串切换

14.11 switch 语句

表达式的类型必须是 char、byte、short、int、Character、Byte、Short、Integer 或枚举类型 (§8.9),否则会出现编译时错误。

通常,当您需要打开字符串值时,通常可以通过使用 char(因为字符串只有一个字符长)或枚举来解决此限制。在你的情况下,枚举看起来更有可能。

This answer is only valid for Java 6 or earlier! Switching on strings has been added in Java 7

14.11 The switch Statement

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type (§8.9), or a compile-time error occurs.

Usually, when you need to switch on a string value, you can often work around this limitation by using char (as the string is only ever going to be one character long) or an enum. In your case, enum looks more likely.

耶耶耶 2024-10-27 03:13:42

是的。直到java 6,还没有使用字符串。
虽然你可以用 ENUMS 来解决这个问题,比如:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
    THURSDAY, FRIDAY, SATURDAY 
}

switch (day) {
        case MONDAY: System.out.println("Mondays are bad.");
                     break;

        case FRIDAY: System.out.println("Fridays are better.");
                     break;

        case SATURDAY:
        case SUNDAY: System.out.println("Weekends are best.");
                     break;

        default:     System.out.println("Midweek days are so-so.");
                     break;
    }

对于我们人类来说更容易阅读,对吧?

源是 http://download.oracle.com/javase/tutorial/java /javaOO/enum.html

Yes. Until java 6, not with Strings.
Tough you can do a workaround with ENUMS, something like:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
    THURSDAY, FRIDAY, SATURDAY 
}

switch (day) {
        case MONDAY: System.out.println("Mondays are bad.");
                     break;

        case FRIDAY: System.out.println("Fridays are better.");
                     break;

        case SATURDAY:
        case SUNDAY: System.out.println("Weekends are best.");
                     break;

        default:     System.out.println("Midweek days are so-so.");
                     break;
    }

more easy to read for us humans, right?

source is http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

默嘫て 2024-10-27 03:13:42

实际上,根据 Java SE 7 中的 Oracle,您可以将字符串与 switch 语句一起使用

http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

public class StringSwitchDemo {

public static int getMonthNumber(String month) {

    int monthNumber = 0;

    if (month == null) { return monthNumber; }

    switch (month.toLowerCase()) {
        case "january":    monthNumber =  1; break;
        case "february":   monthNumber =  2; break;
        case "march":      monthNumber =  3; break;
        case "april":      monthNumber =  4; break;
        case "may":        monthNumber =  5; break;
        case "june":       monthNumber =  6; break;
        case "july":       monthNumber =  7; break;
        case "august":     monthNumber =  8; break;
        case "september":  monthNumber =  9; break;
        case "october":    monthNumber = 10; break;
        case "november":   monthNumber = 11; break;
        case "december":   monthNumber = 12; break;
        default:           monthNumber =  0; break;
    }

    return monthNumber;
}

public static void main(String[] args) {

    String month = "August";

    int returnedMonthNumber =
      StringSwitchDemo.getMonthNumber(month);

    if (returnedMonthNumber == 0) {
      System.out.println("Invalid month");
    } else {
      System.out.println(returnedMonthNumber);
    }
}

}

Actually, according to Oracle in Java SE 7 you can use strings with the switch statement

http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

public class StringSwitchDemo {

public static int getMonthNumber(String month) {

    int monthNumber = 0;

    if (month == null) { return monthNumber; }

    switch (month.toLowerCase()) {
        case "january":    monthNumber =  1; break;
        case "february":   monthNumber =  2; break;
        case "march":      monthNumber =  3; break;
        case "april":      monthNumber =  4; break;
        case "may":        monthNumber =  5; break;
        case "june":       monthNumber =  6; break;
        case "july":       monthNumber =  7; break;
        case "august":     monthNumber =  8; break;
        case "september":  monthNumber =  9; break;
        case "october":    monthNumber = 10; break;
        case "november":   monthNumber = 11; break;
        case "december":   monthNumber = 12; break;
        default:           monthNumber =  0; break;
    }

    return monthNumber;
}

public static void main(String[] args) {

    String month = "August";

    int returnedMonthNumber =
      StringSwitchDemo.getMonthNumber(month);

    if (returnedMonthNumber == 0) {
      System.out.println("Invalid month");
    } else {
      System.out.println(returnedMonthNumber);
    }
}

}

软糖 2024-10-27 03:13:42

switch 语句不能与字符串一起使用。在错误列表下......

“不要屏住呼吸”

https:// bugs.java.com/bugdatabase/view_bug?bug_id=1223179

尽管在查看另一个答案中发布的即将发布的文档后,我认为该功能在 JDK7 中可用。

The switch statement cannot work with strings. Under the bug listing....

"Don't hold your breath"

https://bugs.java.com/bugdatabase/view_bug?bug_id=1223179

although after looking at the upcoming docs posted in another answer, I suppose this feature is available in JDK7.

谎言 2024-10-27 03:13:42

Java 的教程说它适用于基本类型(byte、short、char、int)以及字符串。请参阅 http://download.oracle.com/javase/tutorial/java/ nutsandbolts/switch.html

Java's tutorial says it works with primitive types (byte, short, char, int) as well as strings. See http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

我的鱼塘能养鲲 2024-10-27 03:13:42

在 java 7 中,您可以将 switch 与字符串一起使用。请查看此处

with java 7 you can use switch with strings. look here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文