# Negative

• March 6th, 2011, 04:39 PM
xew123
Negative
for some reason when i run my code for fahrenheit to celcius and vise versa when the formula becomes negative it just shows up 0

here is my code

code
Code Java:

```import java.util.Scanner; import java.util.Random;   public class tempature { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Random random = new Random();   double c; double f; int x;   System.out.println("Do you want Celcius(1) or Fahrenheit(2)"); x = scan.nextInt();     if (x == 1){ System.out.println("type the tempature in Celcius."); c = scan.nextInt();   f = (c * 9 / 5 + 32);   System.out.println("Your Fahrenheit tempature is " + f); } else { System.out.println("type the tempature in Fahrenheit."); c = scan.nextInt();   f = ((c - 32) * (5 / 9));   System.out.println("Your Celsius tempature is " + f);   }   } }```
• March 6th, 2011, 06:25 PM
helloworld922
Re: Negative
The problem is because you're code is using integer math, not floating point math. Try:

Code Java:

```import java.util.Scanner; import java.util.Random;   public class tempature { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Random random = new Random();   double c; double f; int x;   System.out.println("Do you want Celcius(1) or Fahrenheit(2)"); x = scan.nextInt();     if (x == 1){ System.out.println("type the tempature in Celcius."); c = scan.nextInt();   f = (c * 9. / 5. + 32); // need the dot to tell java to treat this number as a floating point constant System.out.println("Your Fahrenheit tempature is " + f); } else { System.out.println("type the tempature in Fahrenheit."); c = scan.nextInt();   f = ((c - 32) * (5. / 9.)); // need the dot to tell java to treat this number as a floating point constant System.out.println("Your Celsius tempature is " + f);   }   } }```