有谁知道如何将这个if-then-else语句转换为if-then语句?这太令人困惑了。在哪里放置else部分并确保结构与if-then-else语句相同?谢谢你的帮助。
if ((symptom1.equalsIgnoreCase("Yes"))) //fever 3 to 14 days
{
weight = 0.75; //cf
if ((symptom2.equalsIgnoreCase("Yes"))) //rash on any part of the body
{
weight = 0.55; //cf
if ((symptom3.equalsIgnoreCase("Yes"))) //muscle pain
{
weight = 0.45; //cf
if ((symptom4.equalsIgnoreCase("Yes"))) //low blood pressure
{
weight = 0.38; //cf
if ((symptom5.equalsIgnoreCase("Yes"))) //bleeding gum
{
weight = 0.48; //cf
if ((symptom6.equalsIgnoreCase("Yes"))) //bloody feces
{
weight = 0.35; //cf
//severe
newWeight = 0.7 * 0.35; //cf for disease = 0.7 [min=0.35]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, resultSevereDengue.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
else //consult doctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//consultDoctor
else
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//uncomplicated
else
{
newWeight = 0.6 * 0.3; //cf for disease = 0.6 [min=0.3]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, resultUncomplicatedDengue.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//consultDoctor
else
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//consultDoctor
else
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//absent dengue
else if ((symptom1.equalsIgnoreCase("No"))) //fever 1 to 3 days
{
weight = 0.7; //cf
if ((symptom2.equalsIgnoreCase("No"))) //rash on any part of the body
{
weight = 0.5; //cf
if ((symptom3.equalsIgnoreCase("No"))) //muscle pain
{
weight = 0.4; //cf
if ((symptom4.equalsIgnoreCase("No"))) //low blood pressure
{
weight = 0.3; //cf
if ((symptom5.equalsIgnoreCase("No"))) //bleeding gum
{
weight = 0.4; //cf
if ((symptom6.equalsIgnoreCase("No"))) //bloody feces
{
weight = 0.35; //cf
//absent
newWeight = 0.8 * 0.3; //cf for disease = 0.8 [min=0.3]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, resultAbsentDengue.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
else //consultDoctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
else //consultDoctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
else //consultDoctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
else //consultDoctor
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
//consultDoctor
else
{
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
}
答案 0 :(得分:1)
你应该采取任何体面的IDE,并自己做这项工作。以下是提示:
1)找到重复的代码块,例如
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
并将其移至类似startCfDiseaseIntent
的方法。
2)删除你的评论,并用自描述代码替换它们:
symptom1.equalsIgnoreCase("Yes"))) //fever 3 to 14 days
应该提取到局部变量
boolean isFeverMoreThanTwoDays = symptom1.equalsIgnoreCase("Yes");
然后重复使用。
你仍然可以留下评论,但接近boolean
。
此外,你可以结合新的布尔值来创建新的自我描述的布尔值,例如:
boolean patientHasExtremelyDangerousInfection = isFever... && isMusclePainPresent && isGumBleeding && ...;
然后用它们做出决定。
3)找到不必要的代码:
在下半部分,symptom1.equalsIgnoreCase("No")
所有其他块都相同,因此您可以将所有检查合并到&&
,并删除整个if-then-else
树。
在上半部分:我的IDE显示几乎所有对weight = ...; // cf
的调用都被覆盖,而不会被中间读取,因此那里有很多冗余线 - 使它们有用或删除。
4)必要时创建测试。您的代码使用7个变量,可以是“是”或“否”。这使得2 ^ 7输入变化,这是一个小数字。您可以轻松创建一个执行所有这些变体的程序,消耗生成cf
并将该数据放入某个文件中。然后在该文件的帮助下,您可以确保您的新代码实际上按照旧逻辑运行。
答案 1 :(得分:1)
我认为症状N的值必须为“是”或“否”。
static boolean check(String[] symptoms, String... values) {
if (values.length != symptoms.length) {
throw new IllegalArgumentException();
}
for (int i = 0; i < symptoms.length; ++i) {
if (!symptoms[i].equalsIgnoreCase(values[i])) {
return false;
}
}
return true;
}
和
String[] symptoms = { symptom1, symptom2, symptom3, symptom4, symptom5, symptom6 };
if (check(symptoms, "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")) {
weight = 0.35; //cf
//severe
newWeight = 0.7 * 0.35; //cf for disease = 0.7 [min=0.35]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, resultSevereDengue.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
if (check(symptoms, "Yes", "Yes", "Yes", "Yes", "Yes", "No")) {
weight = 0.3; //cf min
newWeight = 0 * 0.3; //cf for disease = 0
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultDengue1.this, consultDoctor.class);
intent.putExtra("cfDisease", cf);
startActivity(intent);
}
if (check(symptoms, "Yes", "Yes", "Yes", "Yes", "No", "Yes")) {
// ...