简体   繁体   English

Java处理异常后程序完成

[英]program finishes after exception is handled in Java

This is for homework. 这是为了功课。

I am trying to create a program that takes the average of ten input numbers. 我正在尝试创建一个平均需要输入10个数字的程序。 When the user enters a character that is not a number, the NumberFormatException exception is caught. 当用户输入非数字字符时,将捕获NumberFormatException异常。 The program was finishing after the exception was caught, so I changed it to use recursion to call the method again after the exception is caught, but now it prints multiple averages, some of which are not correct. 捕获到异常后程序正在完成,因此我将其更改为在捕获到异常后使用递归再次调用该方法,但是现在它会打印多个平均值,其中一些不正确。

How do I change the program so that it continues to ask for input after an exception has been caught instead of finishing? 如何更改程序,以便在捕获到异常而不是完成之后继续请求输入? I do not want the program to finish after catching the exception. 我不希望程序在捕获异常后完成。

import java.util.Scanner;

import java.util.Scanner;

public class PrintAverage {

    int average;

    public static void main(String args[]) {
        System.out.println("You are going to enter ten numbers to find their average.");
        getInput();
    }

    private static void getInput() {
        String input;
        int sum = 0;
        int[] arrayOfIntegers = new int[10];
        double average = 0;
        Scanner scanner = new Scanner(System.in);

        try {
            for (int i = 0; i < arrayOfIntegers.length; i++) {
                System.out.println("Enter the next number.");
                input = scanner.nextLine();
                arrayOfIntegers[i] = Integer.parseInt(input);
            }
        } catch (NumberFormatException exception) {
            System.out.println("The last entry was not a valid number.");
            getInput();
        }

        for (int k = 0; k < arrayOfIntegers.length; k++) {
            sum = sum + arrayOfIntegers[k];
        }
        average = sum / arrayOfIntegers.length;
        System.out.println("The average is " + average);

    }
}

Your try/catch is not specific enough, so it catches the exception after asking all the numbers you want. 您的try / catch不够具体,因此在询问所有想要的数字后会捕获异常。

  1. Identify the line that is throwing the NumberFormatException 识别引发NumberFormatException的行
  2. Move your try/catch around it 在其周围移动尝试/捕捉
  3. Store the input in a temporary variable that can be null (primitive int don't allow that) 将输入存储在可以为null的临时变量中(primitive int不允许这样做)
  4. Keep asking for a value as long as it's null (= as long as it's not been set to a legit number) 只要它是空值,就一直询问它(=,只要它没有被设置为合法数字)

Localize the exception. 本地化异常。 Change this: 更改此:

try {
        for (int i = 0; i < arrayOfIntegers.length; i++) {
            System.out.println("Enter the next number.");
            input = scanner.nextLine();
            arrayOfIntegers[i] = Integer.parseInt(input);
        }
    } catch (NumberFormatException exception) {
        System.out.println("The last entry was not a valid number.");
        getInput();
    }

to this: 对此:

    for (int i = 0; i < arrayOfIntegers.length; i++) {
        System.out.println("Enter the next number.");
        input = scanner.nextLine();
        try {
           arrayOfIntegers[i] = Integer.parseInt(input);
        } catch (NumberFormatException exception) {
            System.out.println("The last entry was not a valid number.");
            i--; //so you don't lose one of the 10.
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM