import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean x;
Scanner sc = new Scanner(System.in);
String igual = sc.next().toString();
String[] yes = new String[15];
yes[0]="When I find myself in times of trouble";
yes[1]="Mother Mary comes to me";
yes[2]="Speaking words of wisdom";
yes[3]="Let it be ";
yes[4]="And in my hour of darkness ";
yes[5]="She is standing right it front of me ";
yes[6]="mama just killed a man";
yes[7]="And when the broken hearted people ";
yes[8]="Living in the world agree ";
yes[9]="There will be an answer ";
yes[10]="For though they may be parted";
yes[11]="there is still a chance that they will see";
yes[12]="And when the night is cloudy";
yes[13]="There is still a light that shines on me";
yes[14]="Shine until tomorrow";
String[] no = new String[5];
no[0]="I wake up to the sound of music";
no[1]="Mother Mary comes to me";
no[2]="put a gun against his head";
no[3]="pulled my trigger now his dead";
no[4]="mama life had just began";
// searches in the yes array
for (int i=0 ; i<yes.length ; i++){
x=igual.trim().equalsIgnoreCase(yes[i].trim());
if (x=true){
System.out.println("true");
}
}
//searches in the no array
for (int j=0 ; j<no.length ; j++){
x = igual.trim().equalsIgnoreCase(no[j].trim());
if (x=true){
System.out.println("false");
}
}
}
}
即使您输入的字符串仅等于数组中的一个字符串,也会打印 15 次真和 5 次假。我调试了代码,这些就是结果看起来它在“if”条件内设置了“x”变量,提前谢谢你。