2

你好如何在我的android项目中使用switch循环?我想使用安卓 2.1

我需要 JRE 1.7,但我想使用 Android 2.1

我使用这样的循环:

switch ((CHAR[Math.abs(intGen.nextInt()%2)])) {
    case "+":
        result = random2 + random3;
        break;
    case "-":
        result = random2 + random3;
        break;
} 

日志猫:

Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
4

3 回答 3

5

简而言之:目前没有办法,Android SDK 不是 Oracle JDK。并非所有功能都受支持,AFAIKString对 switch 语句的支持不在受支持的功能之列。

恐怕您无法使用旧的equals

if(myString.equals("+")){
 result = random2 + random3;
}
else if (myString.equals("-"))
 result = random2 - random3;
)
于 2012-09-03T11:47:14.950 回答
1

你不能。android只有jre6。您需要以其他方式执行此操作。一种简单的方法是让 CHAR 实际上是可切换的字符。

于 2012-09-03T11:47:25.853 回答
0

陷入同样的​​问题,将 switch-case 更改为 if-else 解决了问题。java 1.7 之前的 switch-case 支持原始类型(boolean、char、short、byte、int)和枚举,在 java 1.7 中引入了 Strings 的切换。另一种方法是将 switch(String) 更改为 switch(int)。

于 2014-05-02T09:32:02.873 回答