gpt4 book ai didi

Java输入不识别字符串

转载 作者:行者123 更新时间:2023-11-29 06:05:37 25 4
gpt4 key购买 nike

对于下面的代码,如果我在键盘上输入y,它会进入分支语句的else部分。这是为什么?

    public static void getInput(){
String response;
String[] coins = new String[6];

System.out.println("Would you like to enter your own amount? (y/n)");

Scanner sc = new Scanner(System.in);
response=sc.nextLine();

if (response=="y"){
System.out.println("You entered y");
}
else{
System.out.println("You did not enter y, you entered " + response);
}
}

最佳答案

你想要 response.equals("y")。

关于Java输入不识别字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8632958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com