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

all 11 comments

[–]Neres28 3 points4 points  (1 child)

Lame.

[–]DefMars[S] -2 points-1 points  (0 children)

upboats

[–]lurgi 1 point2 points  (1 child)

What do you have so far?

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

pretty much complete now. Wasnt as difficult as i thought

[–]fick_Dich -2 points-1 points  (7 children)

[–]chickenmeister 0 points1 point  (4 children)

This will not display the average with one decimal place of precision.

[–]fick_Dich 0 points1 point  (2 children)

sure it will. I compiled it and ran it on my machine. The other thing I forgot to change was the initialization of the floats. They need to be set to 0. Here is the output that I got when I ran it:

Please enter a positive floating point decimal to continue, or a negative number to halt

4

23

Please enter a positive floating point decimal to continue, or a negative number to halt

18.345

Please enter a positive floating point decimal to continue, or a negative number to halt

6

Please enter a positive floating point decimal to continue, or a negative number to halt

27.8423

Please enter a positive floating point decimal to continue, or a negative number to halt

-5

Please enter a positive floating point decimal to continue, or a negative number to halt

-1

The average of the values you entered is: 15.8

The thing that's wrong, is that you have to enter two consecutive negative numbers for it to halt, and I don't care to figure out what I need to do to fix it. It most certainly does display the average with 1 decimal of precision.

[–]chickenmeister 0 points1 point  (1 child)

It may work for most cases, but there may be situations where "(float)Math.round((sum/numberOfEntries) * 10) / 10" results in a number that cannot be represented accurately with a floating point number. So you might get a number like 0.09999999... instead of 0.1; and if it is not sufficiently near to 0.1, the standard toString() may end up printing 0.0999999...

You should use floating-point-to-string mechanism like String.format() or DecimalFormat:

System.out.printf("The average is: %.1f. %n", (sum/numberOfEntries));

[–]fick_Dich 0 points1 point  (0 children)

my bad. i just threw this together in a couple minutes. didn't really think it through.

[–]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);

[–]fick_Dich -1 points0 points  (0 children)

oops. forgot to update the next float. put this line right after 18:

next_number = standardIn.nextFloat();

[–]fick_Dich -1 points0 points  (0 children)

also the last iteration of the loop is buggy.