gpt4 book ai didi

java - 如果条件为真则停止代码

转载 作者:行者123 更新时间:2023-12-02 05:22:42 25 4
gpt4 key购买 nike

问题出在while循环上,有注释

BufferedReader user = new BufferedReader(new FileReader(
"C:\\Users\\Ionut\\workspace\\tBot\\persoane.txt"));

String line;
while ((line = user.readLine()) != null) {
if (line.toLowerCase().contains(nume.toLowerCase())) {
System.out.println("Ce mai faci " + nume + "?");
ceva = scanIn.nextLine(); //here i want to stop if the condition is true, but it will show the message and go ahead and execute the rest of the code
}
}

最佳答案

基本上有两种常见的解决方案:

1-使用break:

while ((line = user.readLine()) != null) {
if (line.toLowerCase().contains(nume.toLowerCase())) {
System.out.println("Ce mai faci " + nume + "?");
ceva = scanIn.nextLine();
break; // exists the closest loop
}
}

2- 使用 boolean 标志:

boolean stop = false;
while (!stop && (line = user.readLine()) != null) {
if (line.toLowerCase().contains(nume.toLowerCase())) {
System.out.println("Ce mai faci " + nume + "?");
ceva = scanIn.nextLine();
stop = true;
}
}

关于java - 如果条件为真则停止代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26369522/

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