1

好的,所以我正在学习一个 CS 安全课程,其中包含一些基本的 java 编程,我们的第一个任务是玩BigInteger. 但是,我们还必须对我们的程序进行“防弹”。虽然我的方法不是最理想的,但它适用于第一个输入。Aka 如果我为我的任何程序输入无效输入,input.new---();将提示用户使用有效数字再试一次。但是第一个input.nextInt();仍然会接受无效输入并崩溃,显示"java.util.InputMismatchException"然后漫无目的的扫描仪。关于为什么会发生这种情况的任何想法?ps 程序在任何情况下都不得显示错误日志。

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) <-----");
      }

    } 
  }
}
4

4 回答 4

0

您的catch块仅处理NumberFormatException. 如果你想处理所有Exception(s)那么我建议你改变这个

catch (NumberFormatException e){

类似于

catch (Exception e){
于 2015-01-29T13:34:30.443 回答
0

nextDouble()将读取双精度值。如果 parseDouble 失败,InputMismatchException则抛出 a。

这里是类的代码:

public double nextDouble() {
    // Check cached result
    if ((typeCache != null) && (typeCache instanceof Double)) {
        double val = ((Double)typeCache).doubleValue();
        useTypeCache();
        return val;
    }
    setRadix(10);
    clearCaches();
    // Search for next float
    try {
        return Double.parseDouble(processFloatToken(next(floatPattern())));
    } catch (NumberFormatException nfe) {
        position = matcher.start(); // don't skip bad token
        throw new InputMismatchException(nfe.getMessage());
    }
}
于 2015-01-29T13:35:03.623 回答
0

甲骨文说

公共类 InputMismatchException 扩展 NoSuchElementException 由 Scanner 抛出以指示检索到的令牌与预期类型的​​模式不匹配,或者该令牌超出预期类型的​​范围。

公共类 NumberFormatException 扩展 IllegalArgumentException 抛出该异常以指示应用程序已尝试将字符串转换为数字类型之一,但该字符串没有适当的格式。

你只NumberFormatException在 catch 块中使用过,所以它只捕获NumberFormatException. 要捕获其他异常,您必须添加其他 catch 块

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) <-----");
于 2015-01-29T13:35:13.900 回答
0

InputMismatchException并且NumberFormatException是不同的类别。它们之间没有关系,你的程序抛出InputMismatchException并且你试图捕捉NumberFormatException。您将需要一个额外的 catch 块来处理InputMismatchException异常。

于 2015-01-29T13:36:05.407 回答