gpt4 book ai didi

java - 使用扫描仪捕获/尝试不处理输入不匹配异常?

转载 作者:太空宇宙 更新时间:2023-11-04 14:11:54 25 4
gpt4 key购买 nike

好吧,我正在学习 CS 安全类(class),其中包含一些基本的 Java 编程,我们的第一个作业是使用 BigInteger。然而,我们还必须“防弹”我们的程序。虽然我的方法不是最理想的,但除了第一个输入之外,它都有效。也就是说,如果我为任何 input.new---(); 输入无效的输入,我的程序将提示用户使用有效数字重试。但第一个 input.nextInt(); 仍然会接受无效输入并崩溃,显示 “java.util.InputMismatchException”,然后漫无目的地谈论扫描仪。关于为什么会发生这种情况有什么想法吗?附注程序在任何情况下都不得显示错误日志。

import java.io.*;
import java.util.*;
import java.math.*;

class BigNumber{
public static void main(String args[]){

while(true){
try{
Scanner input = new Scanner(System.in);

System.out.print("if you wish to exit the program type in '0,' to continue running the program type in any other value: ");
double esc= input.nextDouble();
if(esc == 0){ break;}
else{System.out.println("Ok, program running...");}
input.nextLine();

System.out.print("Enter number a: ");
String aValue = input.nextLine();

System.out.print("Enter number b: ");
String bValue = input.nextLine();

System.out.print("Enter a number 'n': ");
String nValue = input.nextLine();
while (Integer.parseInt(nValue) < 1)
{
if (Integer.parseInt(nValue) < 1)
{
System.out.print("Please enter a valid number (n>1): ");
nValue = input.nextLine();
}
}

BigInteger a= new BigInteger(aValue);
BigInteger b= new BigInteger(bValue);
BigInteger n= new BigInteger(nValue);

System.out.println("---------------------------");
System.out.println("1) a xor b: " + a.xor(b));
System.out.println("2) b xor b: " + b.xor(b));
System.out.println("3) a xor b xor a: " + a.xor(b).xor(a));
System.out.println(" ");
System.out.println("4) ab mod n: " + a.modPow(b,n));
System.out.println(" ");
System.out.println("5) a shifted to the right by 6: " + a.shiftRight(6));
System.out.println("6) b shifted to the left by 3: " + b.shiftLeft(3));
System.out.println("---------------------------");

}
catch (NumberFormatException e){
System.out.println("-----> Please try entering a valid number(s) <-----");
}

}
}
}

最佳答案

甲骨文说

public class InputMismatchException extends NoSuchElementException Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

public class NumberFormatException extends IllegalArgumentException Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

您在 catch block 中仅使用了 NumberFormatException,因此它仅捕获 NumberFormatException。要捕获其他异常,您必须添加其他 catch block

catch (NumberFormatException e){
System.out.println("-----> Please try entering a valid number(s) <-----");
catch (InputMismatchException e){
System.out.println("-----> Please try entering a valid number(s) <-----");

或如果您使用基异常类它将捕获任何类型的异常

catch (Exception e){
System.out.println("-----> Please try entering a valid number(s) <-----");

关于java - 使用扫描仪捕获/尝试不处理输入不匹配异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28215972/

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