Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. 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
```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);

}

}
}```

2. Re: Negative

The problem is because you're code is using integer math, not floating point math. Try:

```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);

}

}
}```