java - 异常处理在运行程序中只起作用一次

标签 java exception formatexception

最初,我尝试将 try 语句放入 while 循环中,但是遇到了几个错误。该程序运行完美,除了当我输入不规则字符时,一旦它给出了我输入的我创建的打印行,但是,当我再次输入另一个字符时,该行不会弹出,而是给我一个格式异常错误。

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
  }
}

最佳答案

你想要一些东西来捕获 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
  }

关于java - 异常处理在运行程序中只起作用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15099583/

相关文章:

java - Spring Data JPA 与远程 MySQL 服务器的 ssh 隧道

c++ - 为什么我的析构函数异常没有被捕获

c# - 为什么 System.Convert ("0") 在某些系统上抛出 FormatException?

java - 将 java.util.Date 增加一天

java - 从java中的文件中提取数值

Java:比较两个音频文件看它们是否相同 "music"

c# - 为什么我的每一个设计 View 中都会出现 XAML 设计器错误?

Java对象序列化,无法关闭ObjectOutputStream?

c# - Entity Framework - 索引(从零开始)必须大于或等于零

c# - 如何解决asp.net中的格式异常错误?