Java Can';如果语句不起作用

Java Can';如果语句不起作用,java,Java,可能重复: 我在10分钟前让它工作,改变了一些东西,但现在它因为某种原因没有显示“它们是一样的”?这很简单,但我看不出哪里出了问题 谢谢 操作符通过引用比较对象 要了解两个不同的字符串实例是否具有相同的值,请调用.equals() 因此,更换 if (word1 == word2) 与 =操作符通过引用比较对象 要了解两个不同的字符串实例是否具有相同的值,请调用.equals() 因此,更换 if (word1 == word2) 与 请尝试此操作,它将起作用,字符串不是基元,因此当您检查

可能重复:

我在10分钟前让它工作,改变了一些东西,但现在它因为某种原因没有显示“它们是一样的”?这很简单,但我看不出哪里出了问题


谢谢

操作符通过引用比较对象

要了解两个不同的
字符串
实例是否具有相同的值,请调用
.equals()

因此,更换

if (word1 == word2)


=
操作符通过引用比较对象

要了解两个不同的
字符串
实例是否具有相同的值,请调用
.equals()

因此,更换

if (word1 == word2)


请尝试此操作,它将起作用,
字符串不是基元
,因此当您检查
==
时,它将检查引用

import java.util.Scanner;
/**
 * This program compares two strings
 * @author Andrew Gault
 * @version 28.10.2012
 */
 public class stringComparer
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner (System.in);
        System.out.println ("Enter 1 word here - ");
        String word1 = scan.next();

    System.out.println ("Enter another word here - ");
    String word2 = scan.next();

    if (word1.equals(word2))
    {
        System.out.println("They are the same");
    }

}
}

请尝试此操作,它将起作用,
字符串不是基元
,因此当您检查
==
时,它将检查引用

import java.util.Scanner;
/**
 * This program compares two strings
 * @author Andrew Gault
 * @version 28.10.2012
 */
 public class stringComparer
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner (System.in);
        System.out.println ("Enter 1 word here - ");
        String word1 = scan.next();

    System.out.println ("Enter another word here - ");
    String word2 = scan.next();

    if (word1.equals(word2))
    {
        System.out.println("They are the same");
    }

}
}
使用

查看为什么使用

明白为什么了吗

if (word1.equals(word2))
{
 System.out.println("They are the same");   
}