If your code is missing any of the components below, you will not get any credit. Please read the instructions carefully. Write a program that asks the user to input 5 positive integers. Use a for loop that loops 5 times for this. Have your program throw exceptions if any given values are negative (throw a custom exception). Also, be sure to protect against non-integer inputs (input mismatches). If the user fails on any of the five inputs they will need to input that one again. After 5 successful positive integer inputs report the sum of the five numbers to the screen as well as the average of those five numbers to the screen. The average should be the actual average (a double, not an int). You MUST use a for loop to obtain all 5 values. Do not just write the same code out 5 times. (This is different than program 1!) You MUST total and average the 5 values. You’ll need to find a way to manipulate the for loop in the event the user enters an invalid input, because an invalid input will still cause you to use up one of your iterations in the loop. Make sure the user is forced to correct their bad input immediately after they enter it. If you have them re-type in all 5 numbers each time they have an invalid input, this is not what I want. Also, include in your output which number they are on, as shown below. Tip: Create the for loop first, then put the try/catch structure inside. You will need two catch blocks. The example below is the exact set of test values I will use to test your program but your program should be able to handle ANY input.
-
If your code is missing any of the components below, you will not get any credit. Please read the instructions carefully.
-
Write a
program that asks the user to input 5 positive integers. Use a for loop that loops 5 times for this. -
Have your program throw exceptions if any given values are negative (throw a custom exception). Also, be sure to protect against non-integer inputs (input mismatches).
-
If the user fails on any of the five inputs they will need to input that one again.
-
After 5 successful positive integer inputs report the sum of the five numbers to the screen as well as the average of those five numbers to the screen. The average should be the actual average (a double, not an int).
-
You MUST use a for loop to obtain all 5 values. Do not just write the same code out 5 times. (This is different than program 1!) You MUST total and average the 5 values. You’ll need to find a way to manipulate the for loop in the event the user enters an invalid input, because an invalid input will still cause you to use up one of your iterations in the loop.
-
Make sure the user is forced to correct their bad input immediately after they enter it. If you have them re-type in all 5 numbers each time they have an invalid input, this is not what I want. Also, include in your output which number they are on, as shown below.
-
Tip: Create the for loop first, then put the try/catch structure inside. You will need two catch blocks.
-
The example below is the exact set of test values I will use to test your program but your program should be able to handle ANY input.
Example:
This program will give the sum & average of 5 positive integer.
Please enter number 1:
>>>5
Please enter number 2:
>>>hi
Wrong data type.
Please enter number 2: //asks for number 2 again
>>>4
Please enter number 3:
>>>3
Please enter number 4:
>>>2.5
Wrong data type.
Please enter number 4: //asks for number 4 again
>>>2
Please enter number 5:
>>>-1
Value must be positive. //asks for number 5 again
Please enter number 5:
>>>1
The sum is 15 and the average of your 5 numbers is 3.0.
in java
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images