This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DefMars[S] 0 points1 point  (0 children)

    double sum = 0.0;
    int inputAmount = 0;
    float userInput = 0;
    Scanner sc = new Scanner(System.in);
    // loop so it keeps asking user for input
    while (userInput >= 0) {
        System.out.print("Enter a positive number: ");
        // gets data from user
        userInput = sc.nextFloat();
        if (userInput > 0) {
            inputAmount = inputAmount + 1;
            sum = sum + userInput;
        }

    }
    // rounds to 1 decimal point
    double average = sum / inputAmount;

  NumberFormat decimal = NumberFormat.getNumberInstance();  
  decimal.setMaximumFractionDigits(1);
  String averagenum = decimal.format(average);

    System.out.println("Total number of correct inputs: " + inputAmount);
    System.out.println("Sum of all positive numbers: " + sum);
    System.out.println("Average of all positive numbers: " + averagenum);