gpt4 book ai didi

java - 循环内更新时仅显示一次提示

转载 作者:行者123 更新时间:2023-12-01 17:56:54 24 4
gpt4 key购买 nike

我只想显示一次提示。 “你想更新吗”在调用更新函数的循环内。

for (String person : persons) {
if (id != null && !id.equals(person.getLocalId())) {
System.out.print("ID is not same");
BufferedReader reader = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Do You Want to Update (Y/N) ? >");
try {
var ans = reader.readLine();
if (ans.equalsIgnoreCase("Y") && ans.length() > 0) {
service.update(person.id);
} else if (ans == null || ans.equalsIgnoreCase("N")) {
System.exit(0);
}
} catch (IOException e) {
e.printStackTrace();
}

}
}
sqlSession.commit();

最佳答案

只要满足循环条件,就会执行循环体。对于“foreach”循环的特殊情况,它对集合中的每个元素执行一次。

因此,如果您只想执行一次某件事,则必须将其移出循环。我假设您只想在循环中调用 service.update 方法,而不是所有内容(即您不想从循环内部调用 System.exit(0) )。

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Do You Want to Update (Y/N) ? >");
try {
var ans = reader.readLine();
if (ans.equalsIgnoreCase("Y") && ans.length() > 0) {
for (String person : persons) {
service.update(person.id);
}
} else if (ans == null || ans.equalsIgnoreCase("N")) {
System.exit(0);
}
} catch (IOException e) {
e.printStackTrace();
}

sqlSession.commit();

关于java - 循环内更新时仅显示一次提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60696860/

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