gpt4 book ai didi

java - 字符串问题 - Java

转载 作者:行者123 更新时间:2023-12-01 06:28:17 26 4
gpt4 key购买 nike

我正在为类编写一个程序,在其中分析输入的不同产品代码。这很简单,但我遇到了问题。如果用户输入“E”或“e”,我试图结束循环。然而,它根本没有结束循环。这是在 while 语句的末尾,因此将循环设置为 false 应该结束它,并且它甚至不输出总数,所以我搞砸了一些东西。 Code是字符串类型。

        // Prompt the user for another company code or exit
System.out.print("Enter the company code or type 'e' to exit: ");

// Input the user's company code
code = scan.nextLine();

// Check to see if the user wants to exit
if (code == "e" || code == "E") {
// Output final statistics
System.out.print("Total valid codes: " + valid + "/n");
System.out.print("Total banned codes: " + banned);

// End the loop
loop = false;
}

有什么想法吗?谢谢!

最佳答案

您需要使用code.equals("e") || code.equals("E") (或只是 code.equalsIgnoreCase("e"))。这是因为 == 进行身份比较(“xy是同一个对象吗?”),而equals 是否进行比较(“xy 是否具有相同的值?”)。

关于java - 字符串问题 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6728946/

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