-2

Hey guys I'm a new Java learner. I have a problem with this code: I dont see why it prints only the value of a, and not of ((9/5)*a+32) for example if a = 50 , ((9/5)*a+32) = 82 but when I compile this it prints a=50 and ((9/5)*a+32)=82.

    Scanner tt = new Scanner(System.in);
            a = tt.nextInt();
            System.out.println(a + " degrees °C match  " + ((9/5)*a+32) + "°F");

Can someone explain this to me?!

Abdelaziz Dabebi
  • 1,624
  • 1
  • 16
  • 21

3 Answers3

1

9/5 is being done using integer maths. In integer, 9/5 is 1.

Try:

(9*a)/5+32

or:

(int)((9.0/5)*a+32)
Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
Tim B
  • 40,716
  • 16
  • 83
  • 128
0

try using

 1.80 * a + 32

or

 9 / 5.0 * a + 32

what happens in your code is that you are dividing an integer by integer and

integer / integer = integer

it leads to precision loss..so you have to convert it to float or double type.

rock321987
  • 10,942
  • 1
  • 30
  • 43
0

You are performing integer division, I believe you wanted

System.out.println(a + " degrees °C match  "
    + ((((double) 9) / 5) * a + 32) + "°F");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249