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。
最佳答案
问题是您打开的是字符串 数组 而不是字符串...
switch(args[0])
会工作 - 如果您使用的是 JDK7... 并且有一个参数提供给您的程序 - 否则您会得到一个不错的 ArrayOutOfBoundsException
...
关于java - 我们如何使用字符串进行大小写切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19177873/