android如何从if-else转换为switch-case
我有一个关于pmt函数的应用程序。然而,有太多的情况需要处理。不知何故,该应用程序将无法处理超过12个的数据。我想使用switch-case,但我仍然不太明白如何使用switch-case(自从我第一次尝试使用eclipse以来已经有一个半月了)。任何示例都将受到高度赞赏 下面是我的示例代码:android如何从if-else转换为switch-case,android,Android,我有一个关于pmt函数的应用程序。然而,有太多的情况需要处理。不知何故,该应用程序将无法处理超过12个的数据。我想使用switch-case,但我仍然不太明白如何使用switch-case(自从我第一次尝试使用eclipse以来已经有一个半月了)。任何示例都将受到高度赞赏 下面是我的示例代码: if(String1.toString().equals("condition1")){ //do something if(String2.toStrin
if(String1.toString().equals("condition1")){
//do something
if(String2.toString().equals("condition1.1")&& String3.toString().equals("condition1.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition1.##")&& String3.toString().equals("condition1.##")){
//do something else
}
}
else if(String1.toString().equals("condition2")){
//do something
if(String2.toString().equals("condition2.1")&& String3.toString().equals("condition2.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition2.##")&& String3.toString().equals("condition2.##")){
//do something else
}
}
if(String1.toString().equals("condition3")){
//do something
if(String2.toString().equals("condition3.1")&& String3.toString().equals("condition3.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition3.##")&& String3.toString().equals("condition3.##")){
//do something else
}
}
我想知道,在这种情况下怎么做。或者,如果我们有3乘以3的条件,就有更好的实现。例如a,b,c(假设这三个条件只能使用一次)和d,e,f和g,h,i那么条件1是a,d,g;条件2为a,d,h条件3为a,d,i;条件4a,例如……等等
注意:假设API版本为8-11(旧android)
感谢您,因为支持java 1.7打开字符串 您可以选择两个开关:
switch(String1) {
case "condition1": {
switch(String2) {
case "condition1.1":
break;
// ... other cases
default:
break;
}
}
break;
// ... other cases
default break;
}
答案取决于android的目标版本。从KitKat到更高级别(API级别19+),Java 7的
开关(字符串)
可用。我还强烈建议尝试将子类(条件n.x)分为不同的方法。它很快就会变得非常笨拙,否则:
switch (String1.toString) {
case "condition1":
handleCase1(String2, String3);
break;
case "condition2":
handleCase2(String2, String3);
break;
}
如果这仍然会导致代码过于复杂,则可以尝试将查找表与命令模式结合使用:
我的目标是API 21和minSDK API 8的SDK。另一个问题是,如果用户使用API低于19的设备会发生什么?如果您在
AndroidManifest.xml
中设置
,您的应用程序将不会安装在更低版本的设备上。如果不这样做,您的应用程序将崩溃,因为它使用了设备上的虚拟机不提供的功能。BlackBelt answer呢?Java1.7是Java7的意思,不是吗?Java1.7是Java7的通俗说法。Java从1.4到5.0再到6,再到7到8。在不追赶市场宣传的真正传统中,很多人将Java 7称为Java 1.7,事实上确实如此。很抱歉,我忘了提到我想让它在使用旧版本API的设备上工作。我必须取消勾选所有都是字符串,这就像字符串比较,但在大范围内它会。您必须至少使用android sdk的版本19进行编译。如果您使用的是android studio,例如编译SDK版本19buildToolsVersion“19.0.0”
,那么它需要设备上的API 19才能使用?不需要。您必须使用该版本进行编译。非常抱歉,我忘了提到我想让它在使用旧版本API的设备上工作。我必须取消选中Kit,它也可以在kitkat之前的设备上工作。只需编写一个演示,并使用旧版本的android在模拟器上运行即可
class ConditionKey {
final String String1;
final String String2;
final String String3;
public int hashCode(); // hash strings
public boolean equals(); // compare strings
}
interface ConditionCommand {
// use whatever arguments the operation needs, you can also
// add fields and initialize in the constructor
void perform(final ConditionKey key, /* [...] */);
}
Map<ConditionKey, ConditionCommand> actionMap = new HashMap<>();
actionMap.put(
new ConditionKey("condition1", "condition1.1", "condition1.2"),
new ConditionCommand() {
void perform(final ConditionKey key) {
// perform actions that need to be done
}
}
);
[...]
ConditionKey key = new ConditionKey(string1, string2, string3);
// get the action from the map
ConditionCommand command = actionMap.get(key);
// perform the command
command.perform(key);