我正在尝试创建一个程序,该程序接受用户输入的两个单词并确定这些单词是否相同。
import java.util.Scanner;
public class L7E3 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System. in );
String word1, word2;
System.out.println("Please enter a word: ");
word1 = keyboard.nextLine();
System.out.println("Please enter a word: ");
word2 = keyboard.nextLine();
if (word1 == word2) {
System.out.println("The words are " + word1 + " and " + word2 + ". These words are the same.");
} else {
System.out.println("The words are " + word1 + " and " + word2 + ". These words are not the same.");
}
}
}
我认为 word1==word2 可以确定两个字符串是否相等,我使用的是 JGrasp,无论输入如何,它都会直接进入我的 else 选项。我对字符串做错了吗?