可能重复:
如何比较 Java 中的字符串?
在我的 Java 应用程序中。要接受挑战,请按 y,但程序不会继续。谁能帮我这个?
这是我的代码:
import java.util.Scanner;
public class MainApp
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Hello User");
System.out.println("Please Enter your first name");
String name;
name =scanner.next();
System.out.println("\n" + " Hello " + name + "how are you today?");
String y="";
y=scanner.next();
System.out.println("\n" + " Well " + name + " I am " + y + " Too." +"\n" + " I have a riddle for you " + name + ", wll you Attempt it?" + "\n" + " Type y for yes , or n for no");
String v;
v=scanner.next();
if(v == "y")
{
System.out.println("\n" + "How much wood could a wood chuck chuck if a wood chuck could chuck wood? :) " );
}
else if(v == "n")
{
System.out.println("Ok then " + name + " suit youself, Goodbye! :) ");
}
else
{
System.out.println("Please pick y, or n , and make sure it is in lower case");
}
}
}