Jump to content

New to Java - need help in temperature conversion program! Pl.help!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts
I have done this far.. for a program that converts a temp. from degrees Farenheit to degrees celsius. The program should continue to request input and print converted values until you enter the "sentinel" value of -40, which is the same in both systems and therefore never really needs to be converted. Be sure to tell the user that entering -40 is the way to quit and print the converted value before exiting.

I tried using break; it gave me an error and without break; it gives me an error saying that unreachable statement ...Can anyone help?

import java.util.Scanner;

/**
Convert a temperature in degrees from either celsius to
fahrenheit or fahrenheit to celsius.
 */

public class TempConverter
{
    static Scanner console = new Scanner(System.in);    
    
    public static void main (String[] args)
        {  
        double farenheitTemp,celsiusTemp;
          
                
                while (true) {

            System.out.printf("Enter a temperture in degree Farenheit:");
                    farenheitTemp = console.nextDouble();
                    celsiusTemp = (farenheitTemp *5.0/9.0) +32;
                    System.out.printf("Celsius Temperature: " + celsiusTemp);
                                              
                    System.out.printf("Enter a temperature in degree Celsius:");
                    celsiusTemp = console.nextDouble();
                    farenheitTemp = (celsiusTemp * 9.0/5.0) - 32;
                    System.out.printf("FarenheitTemp:" + farenheitTemp);
                                                }
                    if(farenheitTemp == -40) {
                                             
                      System.out.printf("Quit ");
                                             }
            
                                
         }
}

Edited by ZekeDragon, 03 April 2010 - 02:30 PM.
Please use [code] tags (the # button) when posting code.


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Do
private boolean continue = true;

while(continue){
...
...
if(farenheitTemp == -40) {
  continue = false;
  System.out.printf("Quit ");
}

Oh, and you forgot this part of the assignment :

Quote

Be sure to tell the user that entering -40 is the way to quit.
So simply add a System.out.println() before the while-loop

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your formula for celsius is wrong, too:
F = C*(9/5) + 32
C = (F-32)*(5/9)

Finally, while(true) means the loop will NEVER exit.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts
Thanks for your suggestions. It worked! I was using "break" outside the loop. When I corrected that, it worked fine.
Thanks again.