0

Im trying to make it so that java outputs something like 2.66 when the user inputs 1,2, and 5. However, it just prints 2.00. How can I fix this?

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.print("Please input first integer: ");
    int a = in.nextInt();
    System.out.print("Please input second integer: ");
    int b = in.nextInt();
    System.out.print("Please input third integer: ");
    int c = in.nextInt();
    float calculateInput = (float)((a+b+c)/3);
    System.out.printf("The average of %s, %s, and %s is %.2f \n", a, b, c, calculateInput);

    double e = (double)(1+2+5)/3;
    System.out.print(calculateInput);
}
Allen
  • 11
  • 1
  • try this `(float)(a+b+c)/(float)3;` or double `e = (double)(1+2+5)/( (double))3;` – The Scientific Method Sep 07 '18 at 18:17
  • 2
    By putting parentheses around the math expression in `(float)((a+b+c)/3)`, you're ensuring that integer division is done before the cast to float. Remove the outer parentheses and just do `(float)(a+b+c) / 3`. That will cast the sum of a, b, and c to float, then divide by 3. – Bill the Lizard Sep 07 '18 at 18:19

0 Answers0