0

this is what I have (changed the print message for this question), and when I do 4 and 11, you should get 7.5, but it's only giving me 7. how do I fix this?

import java.util.Scanner;
class U1_L6_Average_Finder {
public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);
 int a;
 int b;
 System.out.println("put in 4");
 a = scan.nextInt();
 System.out.println("Put in 11");
 b = scan.nextInt();
 System.out.println("Here is your average");
 System.out.print((a + b) / 2);
}
Giorgi Tsiklauri
11.5k9 gold badges59 silver badges84 bronze badges
asked Sep 8, 2020 at 19:33
4
  • 5
    Use double or float instead of int. Commented Sep 8, 2020 at 19:35
  • 1
    System.out.print( (double)(a + b) /2 ); Commented Sep 8, 2020 at 19:35
  • 1
    You could also coerce the quotient to a floating-point value by changing the 2 (denominator) to a 2.0, 2f, 2.0f. If you want a double, you can use a d instead of an f. Commented Sep 8, 2020 at 19:39
  • How could I change it so that there is only one input, like a double input instead of two seperate Commented Sep 8, 2020 at 19:40

4 Answers 4

1

You could coerce the quotient to a floating-point value by changing the 2 (denominator) to a 2.0, 2f, 2.0f. If you want a double, you can use a d instead of an f.

public class AverageFloatingPointCoercionExample {
 public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);
 int a, b;
 System.out.print("Enter value #1 (4): ");
 a = scan.nextInt();
 System.out.print("Enter value #2 (11): ");
 b = scan.nextInt();
 System.out.print("Here is your average: ");
 System.out.print((a + b) / 2.0f); // 7.5
 }
}

Alternatively, you can cast the numerator explicitly.

System.out.print(((float) (a + b)) / 2);
answered Sep 8, 2020 at 19:43
Sign up to request clarification or add additional context in comments.

Comments

1

You're using integers (int a, b;), so it will round your results.

Instead of it, use double or float to get what you want, e.g: double a, b;

But, if you don't want to modify the type of your variable, you can edit your last System.out.print using f (for example), that will convert your value to float, e.g:

System.out.print((a + b) / 2.0f);

Check this to understand more about variables: Java Variables

answered Sep 8, 2020 at 19:39

Comments

0
  • You have the input/averaging nailed. So here's the missing bit.
  • Just output r...
  • But if you want a certain number of decimal places (5 here)...
  • I did this in case you wanted to average more than 2 items...

import java.text.DecimalFormat;
class Playground {
 public static void main(String[ ] args) {
 int i = 5;
 int j = 17;
 double r = (double)i/j;
 DecimalFormat df = new DecimalFormat("#.#####");
 System.out.println(r);
 System.out.println(df.format(r));
 }
}

Outputs:

0.29411764705882354

0.29412

answered Sep 8, 2020 at 19:47

Comments

0

You are using integer divison. Integer divison will result integer as answer. You have to change one of the values to double.

Example:

System.out.println((double)(a+b)/2);

or

System.out.println((a+b)/2.0));

answered Sep 8, 2020 at 20:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.