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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Negative

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
     
    	}
     
    	}
    }
    Last edited by helloworld922; March 6th, 2011 at 07:23 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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);
     
    	}
     
    	}
    }

Similar Threads

  1. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  2. Need this to end when a negative number is entered
    By ponyexpress in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:02 AM
  3. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM
  4. [SOLVED] How to make a integer negative if it meets a certain criteria?
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 14th, 2009, 02:27 PM