Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在使用try/catch/finally和error-inputmaschexception时,如何正确实现finally块而不产生错误_Java_Try Catch Finally - Fatal编程技术网

Java 在使用try/catch/finally和error-inputmaschexception时,如何正确实现finally块而不产生错误

Java 在使用try/catch/finally和error-inputmaschexception时,如何正确实现finally块而不产生错误,java,try-catch-finally,Java,Try Catch Finally,是的,我看过类似的问题,没有,我找不到我问题的答案……如果你对我的问题或代码有疑问的话。请问 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 = keyBo

是的,我看过类似的问题,没有,我找不到我问题的答案……如果你对我的问题或代码有疑问的话。请问

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);
问题:

在您向nextDouble输入字符串后,它将使用nextInt的换行符,并将导致InputMismatchException,因为换行符不是Int

解决方案:

在请求用户输入之前,使用catch块中的换行符

样本:

编辑:


问题是什么,它是否编译。立即被否决:这是我得到的锥的错误体积的输出。。。V=1/33.14r^2h输入半径:输入的数据错误。你想再试一次吗?输入“1”以继续,或输入任何其他键以结束:线程主java.util.InputMismatchException位于java.util.Scanner.throwForScanner.java:909位于java.util.Scanner.nextScanner.java:1530位于java.util.Scanner.nextinkscanner.java:2160位于java.util.Scanner.nextinkscanner.java:2119位于volumeConeC.mainvolumeConeC.java:43这符合但一旦我输入了一个“错误的数据”,它会立即在线程中显示我的异常…防炸弹输入有点困难。你是什么级别的?你可能想问你的讲师你可以在这里做什么,如果输入验证不是作业的一部分,他们可能会给你一些代码。我没有讲师可以问。谢谢。我非常感谢。@user2755775单击此帖子上的复选标记,将其标记为已回答。欢迎您,您很高兴编码。Rod_Algonquin…如果您能为我回答,我还有一个不相关的问题…为什么在块对齐混乱之后,如果我放置finally的结尾?@user2755775您不能将其放在while语句之外。避免这样做。你不能把finally放在while循环之外,因为finally和catch一样是try块的一部分。它的作用是确保即使发生错误,也始终执行特定的代码集。将finally放在while之外就像将else放在循环之外。
counter = keyBoard.nextInt ();
         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);