为什么equalsignorecase即使对于非相等字符串也返回true import java.util.Scanner; 公共班机{ 公共静态void main(字符串[]args){ 布尔x; 扫描仪sc=新的扫描仪(System.in); 字符串igual=sc.next().toString(); 字符串[]是=新字符串[15]; 是[0]=“当我发现自己陷入困境时”; 是的[1]=“玛丽妈妈来找我”; 是[2]=“说出智慧的话语”; 是[3]=“顺其自然”; 是的[4]=“在我黑暗的时刻”; 是的[5]=“她就站在我面前”; 是的[6]=“妈妈刚杀了一个人”; 是的[7]=“和当心碎的人”; 是[8]=“生活在这个世界上”; 是[9]=“会有答案”; 是的[10]=“尽管他们可能会分开”; 是[11]=“他们仍然有机会看到”; 是[12]=“当夜晚多云时”; 是的[13]=“还有一盏灯照耀着我”; 是[14]=“明天照耀”; 字符串[]否=新字符串[5]; 否[0]=“我在音乐声中醒来”; 否[1]=“玛丽妈妈来找我”; 否[2]=“用枪顶住他的头”; 否[3]=“他死后扣动了我的扳机”; 否[4]=“妈妈的生活才刚刚开始”; //在“是”数组中搜索 对于(int i=0;i

为什么equalsignorecase即使对于非相等字符串也返回true import java.util.Scanner; 公共班机{ 公共静态void main(字符串[]args){ 布尔x; 扫描仪sc=新的扫描仪(System.in); 字符串igual=sc.next().toString(); 字符串[]是=新字符串[15]; 是[0]=“当我发现自己陷入困境时”; 是的[1]=“玛丽妈妈来找我”; 是[2]=“说出智慧的话语”; 是[3]=“顺其自然”; 是的[4]=“在我黑暗的时刻”; 是的[5]=“她就站在我面前”; 是的[6]=“妈妈刚杀了一个人”; 是的[7]=“和当心碎的人”; 是[8]=“生活在这个世界上”; 是[9]=“会有答案”; 是的[10]=“尽管他们可能会分开”; 是[11]=“他们仍然有机会看到”; 是[12]=“当夜晚多云时”; 是的[13]=“还有一盏灯照耀着我”; 是[14]=“明天照耀”; 字符串[]否=新字符串[5]; 否[0]=“我在音乐声中醒来”; 否[1]=“玛丽妈妈来找我”; 否[2]=“用枪顶住他的头”; 否[3]=“他死后扣动了我的扳机”; 否[4]=“妈妈的生活才刚刚开始”; //在“是”数组中搜索 对于(int i=0;i,java,arrays,string,for-loop,equals,Java,Arrays,String,For Loop,Equals,使用x==true,一个等式表达式,而不是x=true,后者是赋值表达式 JLS,如是说 在运行时,赋值表达式的结果是 赋值后的变量 所以,在代码中 import java.util.Scanner; public class Main { public static void main(String[] args) { boolean x; Scanner sc = new Scanner(System.in); String igual = sc.nex

使用
x==true
,一个等式表达式,而不是
x=true
,后者是赋值表达式

JLS,如是说

在运行时,赋值表达式的结果是 赋值后的变量

所以,在代码中

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");
    }
    }

}

}
x
被赋值
true
,然后
if
计算
true
。因此,无论您从
equalsIgnoreCase
中得到的值是多少,原样语句将始终进入
if
块,因为赋值表达式将返回
true

此外,您不需要对布尔值进行条件检查

if (x = true)

使用相等表达式
x==true
,而不是赋值表达式
x=true

JLS,如是说

在运行时,赋值表达式的结果是 赋值后的变量

所以,在代码中

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");
    }
    }

}

}
x
被赋值
true
,然后
if
计算
true
。因此,无论您从
equalsIgnoreCase
中得到的值是多少,原样语句将始终进入
if
块,因为赋值表达式将返回
true

此外,您不需要对布尔值进行条件检查

if (x = true)

赋值返回其右侧。因此(从
if
-语句条件):


始终返回
true
。您可能正在寻找
x==true
,或者更传统的是,
x
(如
if(x){…}
)。通常应该倾向于使用更简单的第二种变体。

赋值返回其右侧。因此(从
if
-语句条件中):


始终返回
true
。您可能正在查找
x==true
,或者更常规地,查找
x
(如
if(x){…}
)这是Java中出现这种错误的少数几种情况之一——在C中,这种情况更常见,因为C对布尔值的构成有着非常开放的理解……为了避免这种情况,可以使用
true==x
来避免将条件误认为赋值,或者只使用我们e
if(x)
我强烈推荐简单的
if(x){…}
form。这是Java中出现这种错误的少数几种情况之一——在C中,这种情况更常见,因为C对布尔值的构成有着非常开放的理解……要避免这种情况,可以使用
true==x
避免将条件误认为赋值,或者只使用
if(x)
我强烈建议使用简单的
if(x){…}
表单。“然后您的
if
x
评估为true”不,这不是原因。
x
本身的值并不是决定本例中
if
-语句结果的因素。@arshajii我实际上研究过它,除非我误解了它,否则JLS在运行时说,赋值表达式的结果是赋值发生后变量的值。在。是的,这总是正确的,但正如您的回答所暗示的那样,在查找分配给它的变量时不会检索/返回该返回值。换句话说,
如果
没有“检查布尔x的值”赋值之后。@arshajii我不知道如何解释变量的值。如果赋值表达式的结果是变量的值,那么
If
会检查
x
的值。让我试着重新表述一下。您的回答意味着会发生以下情况:右侧的计算结果为
true
>,
x
被分配给
true
x
被查找并返回其值。实际上,这种情况发生了:右侧被计算为
true
,该值被缓存,
x
被分配给
true
,缓存的
true
值被返回(通过“缓存”我的意思是在堆栈上重复)在这里赋值后,
x
永远不会被“查找”。如果您编写类似
int x=1;System.out.println(x=3);
的内容,并通过
javap
查看它,那么您的
if
x
计算为true,则实际上可以在字节码中看到这一点不,这不是原因。
x
本身的值并不是决定本例中
if
-语句结果的因素。@arshajii我实际上研究过它,除非我误解了它,否则JLS在运行时说,赋值表达式的结果是variabl的值