Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Fahrenheit to Celsius Java Program with a while loop. Help?

Here is the teacher directions

Add 40 to the temperature

To convert from F to C, multiply by 5/9

(Note: To convert from C to F, multiply by 9/5 )

Then subtract 40 to obtain the final result

Note: If you literally enter 5/9 in a Java or C program, the result will be 0.

5 and 9 are both integers, so integer (first-grade) arithmetic will be used. In that system 5/9 asks how many complete 9's can be taken from 5, and the answer is 0. Another way to look at it is that 5/9 asks how many 9 dollar tickets can be purchased from 5 dollars. Again: 0. Similarly 12/5 is 2, 12/4 is 3, and 15/4 is also 3.

To get the answer you really want in this case (.55555555......) you have to put a decimal point in at least one of the numbers to force floating-point computation. Thus you could write: 5.0/9 or 5/9.0 or 5.0/9.0 or 5./9. etc.

Create a Scanner object associated with System.In() and use nextInt(), nextDouble() , etc. to obtain an input value from the keyboard. Use System.out.printf() to display the result with the specified rounding

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.

At this point loops have not yet been officially covered in the book. If you don't remember how they work from C, here is some code you can copy. However I have used a poor variable name which you MUST CHANGE to something better in order to get credit for the assignment.

int zz;

while(true) {

//read a new Fahrenheit temp into zz

//convert to Celsius and print the result

//then exit the loop if input was -40

if(zz == -40) {

break;

}

}

Use the tools specified above: You will type in numbers and the program will print results, very much as in the previous assignment. Do NOT use dialog boxes or other graphic tools. You will have a chance to do that later

Here is my code so far. I dont know where to put the while loop and how to quit once someone enters -40. Can someone give me a hint?

import java.util.Scanner;

public class Temperature {

public static void main (String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number : ");

double number = sc.nextDouble();

System.out.println("1.Convert To Fahrenheit");

System.out.println("2.Convert To Celesise");

System.out.print("Pick a choice (1-2): ");

if(sc.nextByte() == 1)

System.out.println(number + " in Celsius equals " + getFahrenheit(number) + " in Fahrenheit");

else

System.out.println(number + " in Fahrenheit equals " + getCelsius(number) + " in Celsius");

System.out.println("\nGood Bye!");

}

public static double getFahrenheit(double c){

return ((c * 9 / 5) + 32);

}

public static double getCelsius(double f){

return ((f - 32) * 5 / 9);

}

}

Update:

To the poster, Thank you!

I am new to java and when I put the code in like this

Scanner sc = new Scanner(System.in);

while(true)

{

//get number here

if(number==-40)

break;

//remaining code

I dont know what the variable should be? because Eclipse keeps giving me an error message. Also do I have to put anything else into the code for this to work?

Update 2:

Here is the final code. Thank you poster for helping me.

import java.util.Scanner;

public class Temperature {

public static void main (String[] args) {

double number = 0;

while(number != -40)

{

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number : ");

number = sc.nextDouble();

if (number == -40) break;

System.out.println("1.Convert To Fahrenheit");

System.out.println("2.Convert To Celesise");

System.out.print("Pick a choice (1-2): ");

if(sc.nextByte() == 1)

System.out.println(number + " in Celsius equals "

+ getFahrenheit(number)

+ " in Fahrenheit");

else

System.out.println(number + " in Fahrenheit equals "

+ getCelsius(number)

+ " in Celsius");

}

1 Answer

Relevance
  • 10 years ago
    Favorite Answer

    +edit

    Just after Scanner declaration

    Scanner sc=new Scanner(System.in );

    while(true)

    {

    System.out.println(" Enter a number ");

    double number = sc.nextDouble();

    if( number==-40)

    break;

    //remaining code

    }

    Where i have written remaining code you have to put your if conditions and full code

    Also as the assignment says you have to not truncate decimal part

    so just put a d after 9 or 5 in each formula

    something like this

    return( (c*9.0/5 )+32);

    same way for other

    +add

    Happy to help you

Still have questions? Get your answers by asking now.