这是我的代码,用于搜索两个调用polyline
方法的位置。当我的editext
Tabaco-Malinao调用方法与Tabaco-Bacacay,Tabaco-Santo Domingo,Tabaco-Malilipot完全相同时。但是,当我的edittext
是Malilipot-Santo Domingo,Santo Domingo-Bacacay时,总是要求第一个条件(Tabaco-Malinao)而不是自己的条件。
条件可能是错误的吗?还是我使用的逻辑运算符?
我的理解是,它采取先发生的事情,而没有正确地采取等号。这可能是错误的。
String origin = etOrigin.getText().toString();
String destination = etDestination.getText().toString();
if (origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco")) {
ttom();
Toast.makeText(getApplicationContext(), "tabaco malinao", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Tabaco")) {
ttob();
Toast.makeText(getApplicationContext(), "tabaco bacacay", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Santo Domingo") && origin.equals("Santo Domingo") == destination.equals("Tabaco")) {
ttosto();
Toast.makeText(getApplicationContext(), "tabaco sto domingo", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Malilipot") && origin.equals("Malilipot") == destination.equals("Tabaco")) {
ttomali();
Toast.makeText(getApplicationContext(), "tabaco malilipot", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Malilipot") == destination.equals("Santo Domingo") && origin.equals("Santo Domingo") == destination.equals("Malilipot")) {
malitosto();
Toast.makeText(getApplicationContext(), "malilipot sto domingo", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Malilipot") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Malilipot")) {
malitobac();
Toast.makeText(getApplicationContext(), "malilipot bacacay", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Santo Domingo") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Santo Domingo")) {
bactosto();
Toast.makeText(getApplicationContext(), "sto domingo bacacay", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Invalid input!", Toast.LENGTH_SHORT).show();
}
最佳答案
逻辑是错误的,假设您有:
String origin = "Malilipot";
String destination = "Santo Domingo";
那么
origin.equals("Tabaco")
是false
并且
destination.equals("Malinao")
也是false
。因此,尽管
origin.equals("Tabaco") == destination.equals("Malinao")
绝对不符合您认为的条件,但false == false
仍为true
。将代码重构为仅使用
&&
(AND)和||
(OR)运算符:if (origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco"))
成为
if ((origin.equals("Tabaco") && destination.equals("Malinao")) || (origin.equals("Malinao") && destination.equals("Tabaco")))