use*_*245 1 java arrays string for-loop equals
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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
即使您输入的字符串仅等于数组中的一个字符串,也会打印15次true和5次false.我对代码进行了调试,结果就是结果看起来它在'if'条件中设置'x'变量提前感谢你.
作业将返回其右侧.因此(从您的 - if陈述条件):
x=true
Run Code Online (Sandbox Code Playgroud)
总是回来true.你可能正在寻找,x == true或者更常规地,x(如if (x) {...}).通常应该优先考虑更简单的第二变体.