import java.util.Scanner; public class Project_5 { public static void main (String[] args) { Scanner input= new Scanner (System.in); int inputNum, sum = 0; int numValid = 0, numInvalid = 0; double average; while (true){ System.out.println ("Enter your values, pressing enter in between each one. When finished press ctrl+d"); if (input.hasNext()); inputNum = input.nextInt(); if (inputNum > 0){ numValid = input.nextInt();} else if (inputNum < 0){ numInvalid = input.nextInt(); System.out.println ("The number " + inputNum + " is invalid.");} System.out.println ("There were " + numValid + " valid numbers entered." + "\nThe sum of the valid numbers was --- and the average was ---." + "\n There were " + numInvalid + " invalid numbers entered.");
So I'm attempting to have this program take the users input of integers, pressing enter in between each one. If they enter a negative number it lets them know that its invalid. At the end of the program it takes all of the valid integers entered and must add them and average them. I've put the final println in there as a placeholder. I'm very lost with my output. If the user types in "6" then presses enter and types in "3" , the output is:
There were 3 valid numbers entered.
The sum of the valid numbers was --- and the average was ---.
There were 0 invalid numbers entered.
It then continues on allowing the user to enter more values. I've tried editing my brackets but that hasn't given me any results.
Here is the example output for correct code"
Enter a positive value (EOF to quit): 4
Enter a positive value (EOF to quit): 7
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 2
Enter a positive value (EOF to quit): -1
The number "-1" is invalid.
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 0
Enter a positive value (EOF to quit): -4
The number "-4" is invalid.
Enter a positive value (EOF to quit): CTRL-D
There were 6 valid numbers entered.
The sum of the valid numbers was 29 and the average was 4.83.
There were 2 invalid numbers.