了解try&;捕获和错误处理 import java.util.Scanner; 公共级Lab4_5{ 公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in); int行=0; int rowIndex=0,colIndex=0; 布尔选择1=真; 字符串y=“y”; 字符串n=“n”; 布尔值优先=真; while(choice1==true){ 如果(first==true){ 第一个=假; System.out.println(“是否要启动(Y/N):”; }else if(first==false){ System.out.println(“是否继续(Y/N):”; } String choice2=scan.next(); 如果(选择2.等于(y)){ System.out.println(“有多少行/列(5-21)?”; rows=scan.nextInt(); while(第21行){ System.out.println(“超出范围或不是整数,请重试!”); rows=scan.nextInt(); } System.out.println(“什么字符?”); String choice3=scan.next(); System.out.println(“”); 对于(rowIndex=1;rowIndex
您需要了解的内容,请查看它 基本认识是了解try&;捕获和错误处理 import java.util.Scanner; 公共级Lab4_5{ 公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in); int行=0; int rowIndex=0,colIndex=0; 布尔选择1=真; 字符串y=“y”; 字符串n=“n”; 布尔值优先=真; while(choice1==true){ 如果(first==true){ 第一个=假; System.out.println(“是否要启动(Y/N):”; }else if(first==false){ System.out.println(“是否继续(Y/N):”; } String choice2=scan.next(); 如果(选择2.等于(y)){ System.out.println(“有多少行/列(5-21)?”; rows=scan.nextInt(); while(第21行){ System.out.println(“超出范围或不是整数,请重试!”); rows=scan.nextInt(); } System.out.println(“什么字符?”); String choice3=scan.next(); System.out.println(“”); 对于(rowIndex=1;rowIndex,java,error-handling,try-catch,Java,Error Handling,Try Catch,您需要了解的内容,请查看它 基本认识是 String choice2=scan.next(); if (choice2.equals(y)) { System.out.println("How many rows/columns(5-21)?"); rows=scan.nextInt(); while (rows<5 || rows>21) { System.out.println("That is either out of range or
String choice2=scan.next();
if (choice2.equals(y)) {
System.out.println("How many rows/columns(5-21)?");
rows=scan.nextInt();
while (rows<5 || rows>21) {
System.out.println("That is either out of range or not an integer, try again! ");
rows=scan.nextInt();
}
}
还有一个finally块,即使出现错误,它也会一直被执行
try {
//Something that can throw an exception.
} catch (Exception e) {
// To do whatever when the exception is caught.
}
在您的特定情况下,您可以有以下例外()
InputMismatchException-如果下一个标记与整数正则表达式不匹配,或超出范围
NoTouchElementException-如果输入用尽
IllegalStateException-如果此扫描仪已关闭
因此,您需要捕获以下异常:
try {
//Something that can throw an exception.
} catch (Exception e) {
// To do whatever when the exception is caught & the returned.
} finally {
// This will always execute if there is an exception or no exception.
}
您可以创建一个try-catch块,如下所示:
try {
rows=scan.nextInt();
} catch (InputMismatchException e) {
// When the InputMismatchException is caught.
System.out.println("The next token does not match the Integer regular expression, or is out of range");
} catch (NoSuchElementException e) {
// When the NoSuchElementException is caught.
System.out.println("Input is exhausted");
} catch (IllegalStateException e) {
// When the IllegalStateException is caught.
System.out.println("Scanner is close");
}
如果您想在代码中实现这一点,我建议您:
try {
int num = scan.nextInt();
} catch (InputMismatchException ex) {
// Exception handling here
}
while(true){
试一试{
rows=scan.nextInt();
如果(第21行){
打破
}
否则{
System.out.println(“超出范围或不是整数,请重试!”);
}
}捕获(输入不匹配异常){
System.out.println(“超出范围或不是整数,请重试!”);
}
}
有关更多详细信息,请参阅。String choice2=scan.next();
while (true) {
try {
rows = scan.nextInt();
if (rows<5||rows>21) {
break;
}
else {
System.out.println("That is either out of range or not an integer, try again! ");
}
} catch (InputMismatchException ex) {
System.out.println("That is either out of range or not an integer, try again! ");
}
}
如果(选择2.等于(y)){
System.out.println(“有多少行/列(5-21)?”;
尝试
{
rows=scan.nextInt();
}捕获(例外e)
{
行=-1;
}
while(第21行){
System.out.println(“超出范围或不是整数,请重试!”);
尝试
{
rows=scan.nextInt();
}捕获(例外e)
{
行=-1;
}
}
您是遇到异常还是只想知道如何让代码运行得更具可读性?-1,您应该很少(如果有的话)直接捕获异常
。-1@Brian S-在这种情况下,他不关心异常是什么,如果有异常,他想重新请求用户输入,这不重要,为什么它是错误的,是的,有捕获异常的案例你是错误的!OP只是学习,所以一个好的答案会教你。伊尚的答案是一个很好的te示例学习如何使用异常:它展示了基本语法和特定用例的完整语法。有些情况下,使用Exception
很好,但大多数情况下,它只是坏代码的拐杖,而这不是你想要传递给新程序员的东西。
while (true) {
try {
rows = scan.nextInt();
if (rows<5||rows>21) {
break;
}
else {
System.out.println("That is either out of range or not an integer, try again! ");
}
} catch (InputMismatchException ex) {
System.out.println("That is either out of range or not an integer, try again! ");
}
}
String choice2=scan.next();
if(choice2.equals(y)){
System.out.println("How many rows/columns(5-21)?");
try
{
rows=scan.nextInt();
}catch(Exception e)
{
rows = -1;
}
while(rows<5||rows>21){
System.out.println("That is either out of range or not an integer, try again! ");
try
{
rows=scan.nextInt();
}catch(Exception e)
{
rows = -1;
}
}