Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class hasnextex
- {
- static Scanner kb = new Scanner(System.in);
- public static void main(String[] args)
- {
- // number entered and the running total
- int inputNum, sum = 0;
- // count of valid and invalid numbers seen
- int numValid = 0, numInvalid = 0;
- int avgl;
- double average;
- System.out.print("Enter a positive value (EOF to quit): ");
- while ( kb.hasNext() )
- {
- inputNum = kb.nextInt();
- if (inputNum >= 0)
- {
- sum = sum + inputNum;
- numValid = numValid + 1;
- }
- else
- {
- System.out.println("The number " + inputNum + " is invalid.");
- numInvalid = numInvalid + 1;
- }
- System.out.print("Enter a positive value (EOF to quit): ");
- }
- avgl = 100 * sum / numValid;
- average = avgl / 100;
- System.out.println("There were " + numValid + " valid numbers entered");
- System.out.printf("The sum of the valid numbers was " + sum + " and the average was " + average + ". ");
- System.out.println("There were " + numInvalid + " invalid numbers.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement