gpt4 book ai didi

java - 可以在 catch block 中访问 try block 内定义的标签吗?

转载 作者:行者123 更新时间:2023-12-02 01:05:37 27 4
gpt4 key购买 nike

try {
do {
input: if (choice2 != null)
System.out.println("Please enter a valid choice");
choice2 = reader.readLine();
} while ((int) choice2.charAt(0) >= 65 && (int) choice2.charAt(0) <= 69);
} catch (StringIndexOutOfBoundsException e) {
continue input;
}

有没有办法实现类似的功能?上面的代码是一个例子。

最佳答案

try-catch移至循环主体中。你可能想给循环贴上标签。而且,即使它们是可选的,也要使用大括号。就像,

input: do {
try {
if (choice2 != null) {
System.out.println("Please enter a valid choice");
}
choice2 = reader.readLine();
} catch (StringIndexOutOfBoundsException e) {
continue input;
}
} while ((int) choice2.charAt(0) >= 65 && (int) choice2.charAt(0) <= 69);

关于java - 可以在 catch block 中访问 try block 内定义的标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60087838/

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