可能重复:
Java 字符串比较?
晚上好。下面的代码没有给出错误,但是当我尝试验证输入的字符串时,它总是假设我没有输入“YES”,即使我确实使用了 toUpperCase() 方法。这一定是一个非常基本的错误,但我现在无法弄清楚,因为我只是在学习这种语言的基础知识。这是代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main{
public static void main(String[] args) throws IOException{
byte counter = 1;
boolean myCondition = false;
String answer;
List<String> myList = new ArrayList<String>();
BufferedReader objReader = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("Enter the #" +counter +" person's name:");
myList.add(objReader.readLine());
counter++;
System.out.println("Would you like to add another name to the list? yes/no");
answer = objReader.readLine();
if (answer.toUpperCase() == "YES"){
myCondition = true;
}
} while (myCondition);
System.out.println("The list of names you entered, looks like this:\n");
for (String name: myList)
System.out.println(name);
System.out.println("\nPress <Enter> to randomize the list of names you entered.");
objReader.readLine();
Collections.shuffle(myList);
System.out.println("Done! \nPress <Enter> to see how the randomized list looks like.\n");
objReader.readLine();
for(String nombre: myList)
System.out.println(nombre);
}
}
提前感谢任何帮助和/或提示。