Java 系统输出困难

Java 系统输出困难,java,system,output,Java,System,Output,因此,我对Java相当陌生,我正在尝试运行一个程序,该程序将显示来自某个名称的一定数量的字母,并要求用户做出响应。用户的回答应该确定两个答案中的一个(“正确”或“对不起,这是不正确的”)。 我遇到的问题是,当我运行程序并输入应该导致“正确”的答案时,我得到的回答是“对不起,那是不正确的。” 我不确定到底发生了什么,但这里有一个链接指向CMD的图片,当我输入应该导致系统说“正确”的内容时,它会说“对不起,这不正确”: 以下是相关代码的副本: System.out.println("\nLet's

因此,我对Java相当陌生,我正在尝试运行一个程序,该程序将显示来自某个名称的一定数量的字母,并要求用户做出响应。用户的回答应该确定两个答案中的一个(“正确”或“对不起,这是不正确的”)。 我遇到的问题是,当我运行程序并输入应该导致“正确”的答案时,我得到的回答是“对不起,那是不正确的。” 我不确定到底发生了什么,但这里有一个链接指向CMD的图片,当我输入应该导致系统说“正确”的内容时,它会说“对不起,这不正确”:

以下是相关代码的副本:

System.out.println("\nLet's play Guess the Celebrity Name.");

String s6 = "Billy Joel";

System.out.println("\n" + s6.substring(2, 7));

Scanner kbReader3 = new Scanner(System.in);
System.out
        .print("\nPlease enter a guess for the name of the above celebrity: ");
String response = kbReader3.nextLine();

System.out.println("\nYou entered: \n" + response + "\n");

if ((response == "Billy Joel")) {
    // Execute the code here if Billy Joel is entered
    System.out.println("\nCorrect!");
} else {
    // Execute the code here if Billy Joel is not entered
    System.out.println("\nI'm sorry, that's incorrect. The right answer was Billy Joel.");
}

System.out.println("\nThank you for playing!");

在这之前,这个程序还有更多的功能,但我对这些都没有问题,而且都是正确的。我去掉了比利·乔尔的部分,其他的一切都按照它应该的那样运行。问题在于上面的代码与它应该输出什么和它正在输出什么有关。我想知道我的代码中是否遗漏了某些内容,或者我输入了错误的内容,但无论我做了什么,都将非常感谢您的帮助。

您的问题就在这里。您使用了错误的运算符来比较字符串

if ((response **==** "Billy Joel")) {
   System.out.println("\nCorrect!");
} else {  ...  }
正确的答案应该是

if ((response.equals("Billy Joel")) {
   System.out.println("\nCorrect!");
} else {  ...  }

要在java中比较字符串,必须使用.equals()运算符。要使用“==”运算符,您需要使用int、bool等。

不要将字符串值与
==
运算符进行比较,请使用
string
equals
方法来比较字符串值。您的链接已断开。在任何情况下,您都可以并且应该在此处发布图像。“Billy Joel”。equals(response)将降低获取空指针异常的风险。@porfiriopartida确实如此
    if (response!=null && response.length>0){
    //trim the input to make sure there are any spaces
    String trimmed=response.trim();
    if (response.equals(s6))
      System.out.println("\nCorrect!");
    } else {  ...  }