【问题标题】:When using try/catch/finally, with error inputmismatch exception, How do I correctly implement a finally block, WITHOUT getting an error使用 try/catch/finally 时,出现错误 inputmismatch 异常,如何正确实现 finally 块,而不会出现错误
【发布时间】:2014-08-30 04:54:46
【问题描述】:

是的,我查看了类似的问题,不,我找不到我的问题的答案...如果您对我的问题或代码有疑问。请询问。

do{
         try {
            System.out.print ("Volume of a cone... V = 1/3(3.14)r^2(h)");
            System.out.println ();
            System.out.print ("Input Radius: ");
            radius = keyBoard.nextDouble ();
            System.out.print ("Input Height: ");
            height = keyBoard.nextDouble ();
         //math
            volume = 0.333 * pie * radius * radius * height;
            System.out.printf ("Volume =   " + volume);
         }//end try
         catch (Exception Error){
            System.out.println ("You Entered the Wrong Data.");
         }
         finally {

            System.out.println ();

            System.out.print ("Do you want to try again?");
            System.out.println ();
            System.out.print ("Input '1' to go again OR any other key to End.: ");
            counter = keyBoard.nextInt ();
         }//end finally

      }while (counter == 1);

【问题讨论】:

  • 问题是什么,它甚至可以编译。立即被否决:P
  • 这是我得到的输出错误,圆锥体积... V = 1/3(3.14)r^2(h) 输入半径:s 你输入了错误的数据。你想再试一次吗?输入“1”以再次进入或任何其他键结束。:在 java.util.Scanner.throwFor(Scanner.java:909) 在 java.util.Scanner.next( Scanner.java:1530) 在 java.util.Scanner.nextInt(Scanner.java:2160) 在 java.util.Scanner.nextInt(Scanner.java:2119) 在 volumeConeC.main(volumeConeC.java:43)
  • 这符合要求,但是一旦我输入“错误数据”,它就会立即在线程中给出我的异常...
  • 防弹输入有点难。你处在哪个等级?你可能想问问你的导师你可以在这里做什么,如果输入验证不是作业的一部分,他们可能会给你一些代码。
  • 我没有要请教的老师。

标签: java try-catch-finally


【解决方案1】:

问题:

counter = keyBoard.nextInt ();

在您向nextDouble 输入字符串后,它将消耗nextInt()newLine 字符,并导致InputMismatchException,因为newLine 不是Int。

解决方案:

在请求用户输入之前,在你的 catch 块中使用 newLine 字符

样本:

         catch (Exception Error){
            System.out.println ("You Entered the Wrong Data.");
            keyBoard.nextLine();
         }

编辑:

boolean tryAgain = false;
    do{
         try {

             if(tryAgain)
             {
                System.out.println ();

                System.out.print ("Do you want to try again?");
                System.out.println ();
                System.out.print ("Input '1' to go again OR any other key to End.: ");
                counter = keyBoard.nextInt ();
                tryAgain = false;
             }

            System.out.print ("Volume of a cone... V = 1/3(3.14)r^2(h)");
            System.out.println ();
            System.out.print ("Input Radius: ");
            radius = keyBoard.nextDouble ();
            System.out.print ("Input Height: ");
            height = keyBoard.nextDouble ();
         //math
            volume = 0.333 * pie * radius * radius * height;
            System.out.printf ("Volume =   " + volume);
            tryAgain = true;
         }//end try
         catch (Exception Error){
            System.out.println ("You Entered the Wrong Data.");
            keyBoard.nextLine();
            tryAgain = true;
         }

      }while (counter == 1);

【讨论】:

  • 谢谢。我真的很感激。
  • @user2755775 单击此帖子上的复选标记以标记为已回答。不客气,编码愉快。
  • Rod_Algonquin...如果您能帮我回答,我还有另一个有点不相关的问题....起来了吗?
  • @user2755775 你不能把它放在while语句之外。避免这样做。
  • 你不能把 finally 放在 while 循环之外,因为和 catch 一样,finally 是 try 块的一部分。即使发生错误,它也可以确保始终执行特定的代码集。将 finally 放在 while 之外就像将 else 放在循环之外。
猜你喜欢
  • 2013-05-20
  • 2013-02-23
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多