这可能是更糟糕的问题,但似乎没有什么对我有用,我在Parse.com中有一个像这样的数据库(由其他人创建):
现在我必须进行一些验证:
如果用户的城市和用户的状态与db的值匹配,则成本将为30
如果用户的州匹配但不匹配城市,则费用为50 否则费用将是100
我写这段代码:
if (ClientList.size() > 0) {
for (int i = 0; i < ClientList.size(); i++) {
final ParseObject p = ClientList.get(i);
String state = p.getString("State");
String city = p.getString("City");
float cost = Float.parseFloat(p.getString("CostInPrecentage"));
if(stateDeli.equalsIgnoreCase(state) && cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
} else if(stateDeli.equalsIgnoreCase(state) && !cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
} else {
shippingCost = cost;
}
}
}
这总是让我退货运费= 100
我试图改变if else
if(stateDeli.equalsIgnoreCase(state)) {
if(cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
} else {
shippingCost = cost;
}
} else {
shippingCost = cost;
}
这也会让我退回运费= 100
如果我像这样做:
if(stateDeli.equalsIgnoreCase(state) && cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
break;
} else if(stateDeli.equalsIgnoreCase(state) && !cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
break;
} else {
shippingCost = cost;
}
它适用于默认和状态,但不适用于匹配州和城市的情况。
除了这些,我尝试了许多条件与休息似乎没有什么工作完美。 你能否建议如何验证这一点。
最后我管理了这样的条件:
if (stateDeli.equalsIgnoreCase(state)&&cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
break;
}
else if (stateDeli.equalsIgnoreCase(state) && ((city == null || city =="" || "Default".equalsIgnoreCase(city)))) {
shippingCost = cost;
break;
}
else if("Default".equalsIgnoreCase(city) && "Default".equalsIgnoreCase(state)){
shippingCost = cost;
break;
}
答案 0 :(得分:-1)
如果您的运费最低,那么您需要确保更高的运费不会超过更低的运费。
if (ClientList.size() > 0) {
for (int i = 0; i < ClientList.size(); i++) {
final ParseObject p = ClientList.get(i);
String state = p.getString("State");
String city = p.getString("City");
float cost = Float.parseFloat(p.getString("CostInPrecentage"));
//if (cost < shippingCost) { // There could be an improvement, otherwise don't try!
if (stateDeli.equalsIgnoreCase(state) && cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
break;
}
else if (stateDeli.equalsIgnoreCase(state) && !cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
break;
}
else if (!stateDeli.equalsIgnoreCase(state) && !cityDeli.equalsIgnoreCase(city)) {
shippingCost = cost;
break;
}
//}
}
}
编辑:答案已更新为使用中断。