简体   繁体   中英

How can I modify my code to print a variable outside, that was initialized inside a try-catch?

This is my code, very simple. It's supposed to get 10 doubles, catch an exception if the user enters something else than a double, then take the average out of the numbers:

import java.util.*;

public class QuestionOne {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double i0, i1, i2, i3, i4, 

i5, i6, i7, i8, i9;

    System.out.println("Please enter a number for each input.");
    try {
        i0 = scan.nextDouble();
        i1 = scan.nextDouble();
        i2 = scan.nextDouble();
        i3 = scan.nextDouble();
        i4 = scan.nextDouble();
        i5 = scan.nextDouble();
        i6 = scan.nextDouble();
        i7 = scan.nextDouble();
        i8 = scan.nextDouble();
        i9 = scan.nextDouble();
        double isum = i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9;
        double iresult = isum / 10;
    } catch (InputMismatchException e) {
        System.out.println(e + "\nPlease, enter only numbers.");
    }

    System.out.println("The average of all the 10 numbers is " + isum);
}
}

So, I'm trying to print isum but obviously can't because it's inside the try-catch. My brain is deep fried after a long day and as I was about to settle down, realized I had this assignment to do. Does anyone have an idea for how should I modify the code to be able to take the average of the 10 doubles and print it?

Don't try to print out the average outside the try/catch; print it inside the try block at the end. When there's an exception thrown you don't want to print the average. The user entered something wrong so even if you could print it the calculation would be invalid.

try {
    i0 = scan.nextDouble();
    i1 = scan.nextDouble();
    i2 = scan.nextDouble();
    i3 = scan.nextDouble();
    i4 = scan.nextDouble();
    i5 = scan.nextDouble();
    i6 = scan.nextDouble();
    i7 = scan.nextDouble();
    i8 = scan.nextDouble();
    i9 = scan.nextDouble();
    double isum = i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9;
    double iresult = isum / 10;

    System.out.println("The average of all the 10 numbers is " + isum);
} catch (InputMismatchException e) {
    System.out.println(e + "\nPlease, enter only numbers.");
}

There won't be any result if there is an exception, so I would print in the try block. Also, according to your print statement you want to print the result (not the sum).

    double iresult = isum / 10;
    System.out.println("The average of all the 10 numbers is " + iresult);
} catch (InputMismatchException e) {
    System.out.println(e + "\nPlease, enter only numbers.");
}

You need to move the print of average within the try block as isum variable can be accessed only within the try block. Try the below code

import java.util.*;

public class QuestionOne {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double i0, i1, i2, i3, i4, 

i5, i6, i7, i8, i9;

    System.out.println("Please enter a number for each input.");
    try {
        i0 = scan.nextDouble();
        i1 = scan.nextDouble();
        i2 = scan.nextDouble();
        i3 = scan.nextDouble();
        i4 = scan.nextDouble();
        i5 = scan.nextDouble();
        i6 = scan.nextDouble();
        i7 = scan.nextDouble();
        i8 = scan.nextDouble();
        i9 = scan.nextDouble();
        double isum = i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9;
        double iresult = isum / 10;
        System.out.println("The average of all the 10 numbers is " + isum);
    } catch (InputMismatchException e) {
        System.out.println(e + "\nPlease, enter only numbers.");
    }
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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