在我的 Android 应用程序中,我正在测试一些字符串(我从另一个 Activity 返回的字符串)以查看它们是否为空。如果没有,我会根据这些值构建我的网址,例如:(已编辑)
//country only
if (!fetchData_country.isEmpty() && fetchData_city.isEmpty() && fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()){
query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country + URL_LANGUAGE;
Log.i("INFO", "Url country only : " + query_url);
}
//country & price min
else if(!fetchData_country.isEmpty() && fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) {
query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country + "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE;
Log.i("INFO", "Url country & price_min : " + query_url);
}
//city & price_min
else if(fetchData_country.isEmpty() && !fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) {
query_url = URL_PRODUCT + "?" + URL_SEARCH_CITY + id_city + "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE;
Log.i("INFO", "Url city & price_min : " + query_url);
}
// country & city & pmin
else if(!fetchData_country.isEmpty() && !fetchData_city.isEmpty() && !fetchData_pmin.isEmpty() && fetchData_pmax.isEmpty()) {
query_url = URL_PRODUCT + "?" + URL_SEARCH_COUNTRY + id_country + "&" + URL_SEARCH_CITY + id_city + "&" + URL_SEARCH_PRICE_MIN + id_pmin + URL_LANGUAGE;
Log.i("INFO", "Url country & city & pmin : " + query_url);
}//many values to test still...
我想知道如何改进这些测试部分,感谢您的帮助!
最佳答案
写一些类似的东西
String query_url = URL_PRODUCT + "?" + (!fetchData_country.isEmpty() ? "URL_SEARCH_COUNTRY + id_country " : "")
+(!fetchData_country.isEmpty() ? "&":"" )+ (!fetchData_pmin.isEmpty() ? " + URL_SEARCH_PRICE_MIN + id_pmin " : "")
+(!fetchData_pmin.isEmpty() ? "&":"" )+ (!fetchData_city.isEmpty() ? " + URL_SEARCH_CITY + id_city " : "");
关于java - 多值测试,如何替换许多else if语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837772/