Java 异常处理在运行的程序中只工作一次

Java 异常处理在运行的程序中只工作一次,java,exception,formatexception,Java,Exception,Formatexception,起初,我试图将try语句放入循环,然而,我遇到了几个错误。该程序运行良好,除了当我输入一个不规则字符时,它给了我我输入的打印行,但是,当我再次输入另一行时,该行不会弹出,而是给了我一个格式异常错误 AddNumbersrealone2.java import java.io.*; // create the class public class AddNumbersrealone2 { // allows i/o public static void main (String []

起初,我试图将
try
语句放入
循环,然而,我遇到了几个错误。该程序运行良好,除了当我输入一个不规则字符时,它给了我我输入的打印行,但是,当我再次输入另一行时,该行不会弹出,而是给了我一个格式异常错误

AddNumbersrealone2.java

import java.io.*;

// create the class
public class AddNumbersrealone2
{
    // allows i/o
  public static void main (String [] args) throws IOException
  {   // initalize variables and strings
    BufferedReader myInput = new BufferedReader  (new InputStreamReader (System.in));
    String sumNumbers;
    //String go;
    double num ;
    double total = 0.0;

    // asks user questions and instructions
    System.out.println("Hello, the following program will ask for your input of a number ");
    System.out.println("Each time you input a number a running total will be added to each previous number")  ;
    System.out.println("Once ready input a number to start!");
    // try and catch block      
    try {

      num = 0;

      // while statement if this occurs stop the program, in this case if a negative integer is inputted

      while (num >= 0) {

        // Contious question asked  
        System.out.println("Input another number..."); 
        sumNumbers = myInput.readLine();
        num = Double.parseDouble (sumNumbers);

        // calculates number (Running total)
        total = total + num;
        System.out.println(total);

        // end error trap
      }
    }
    catch  (Exception e){
      System.out.println("Please refrain from entering regular characters!");
      num = 0;

      // re- while statement if this occurs stop the program, in this case if a negative integer is inputted

      while ( num >= 0) {

        // input question after a character is inputted
        System.out.println("Please input a number: ");
        sumNumbers = myInput.readLine();
        num = Double.parseDouble (sumNumbers);

        total = total + num;
        System.out.println(total);

        // ending statement
      }
    }
    System.out.println("You entered a negative number, the program will exit now");
    System.out.println("Good-bye!");

    // Complete class body
  }
}

您的问题是,一旦抛出第一个异常,您的程序就会陷入catch语句中的while()循环中。因此,如果输入了另一个无效输入,它将在没有try-catch语句的第二个while循环中处理。一个好的修复方法是让try语句只包含num=Double.parseDouble(sumNumbers);。捕获异常时,以continue结束它;语句,以便程序循环回到开始处并请求另一个输入。

是否希望捕获Double.parseDouble周围的异常

比如说

 while(num >= 0)
    {
    // input question after a character is inputted
       System.out.println("Please input a number: ");
       sumNumbers = myInput.readLine();
       try{
            num = Double.parseDouble (sumNumbers);
            total = total + num;
            System.out.println(total);
       } catch(Exception e)
       {
            System.out.println("Please enter a proper number");
       }    


            // ending statement
  }

请重新编写您的第二句话,并向我们提供您的异常的完整堆栈跟踪。似乎您需要重构代码,并引入一个方法(引发异常)从用户输入数据,当您将相同的代码放入catch块作为try块时,这是一个很好的迹象,表明你的逻辑有一些根本性的错误。很难理解你的问题。可能异常来自catch块中的代码。请注意,catch中的大多数代码与try主体相同,也许将这些代码重构为一个方法会使您的代码更清晰?谢谢,它成功了,我非常感谢,但是那些抱怨我的问题不够具体的人。请注意,我是新来的,正在学习一门课程,所以请不要给我太大的压力,因为我在这门课程上是全新的。请移除外部的try-catch块。因此,唯一的try-catch是while循环中的一个。