gpt4 book ai didi

java - 即使评估为真,如果语句也不会执行

转载 作者:行者123 更新时间:2023-11-29 09:39:43 25 4
gpt4 key购买 nike

在下面的代码中,if(timesout[entry] == "exit") 下的 block 永远不会执行。我已经验证 timesout[entry] 在 Debug模式下将当前循环设置为“退出”,并在评估 if 语句之前打印出变量,但无论如何,该 block 当我在提示符下输入 exit 时从不执行,我很困惑为什么。

import java.util.Scanner;

public class timetracker {
public static void main(String args[]) {
boolean exit = false;
String[] reasons = new String[30];
String[] timesout = new String[30];
String[] timesin = new String[30];
int entry = 0;
Scanner keyinput = new Scanner(System.in);

recordloop:
while(exit == false) {
//record info



System.out.println("Enter time out:");
timesout[entry] = keyinput.nextLine();

if(timesout[entry] == "exit") {
exit = true;
break recordloop;
}

System.out.println("Enter reason:");
reasons[entry] = keyinput.nextLine();
System.out.println("Enter time in:");
timesin[entry] = keyinput.nextLine();

entry = entry + 1;

}

System.out.println("Times away from phone:\n ----- \n");
int count = entry;
entry = entry + 1;

while(count < entry) {
System.out.println(reasons[count] + ": " + timesout[count] + " - " + timesin[count] + "\n");
count = count + 1;
}
}
}

最佳答案

timesout[entry] == "exit"

使用equals()比较String,==比较引用相等

查看

关于java - 即使评估为真,如果语句也不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12242587/

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