I have an assignment as follows:
basically using the computer as a calculator to do temp conversions. The relationship between the temperature on the Celsius and Fahrenheit scales is given by this equation:
C / 5 = ( F - 32 ) / 9
perform the following two computations:
Convert 20.50 degree C to F Convert 100.90 degree F to C
Your output will be of the following form:
Temperature 20.5 degree Celsius = ... degree Fahrenheit
Temperature 100.9 degree Fahrenheit = ... degree Celsius
Note the blank line between the two outputs. Your answer will be correct to one place of decimal. Use System.out.printf() to get the correct format.
Here is what I came up. Thanks!
public class Temp
{
public static void main (String[] args)
{
double C = 20.5;
double F = 100.9;
double C / 5 = ( F - 32 ) / 9
System.out.printf("Temperature 20.5 degree Celsius =" + F = %.1f\n, f);
System.out.printf("Temperature 100.9 degree Farentheit =" + C =%.1f\n);
}
}
Java programming, is this correct?
You're close. when using printf() you want to give the whole string with what looks like placeholders and the types that will be placed there. You want something more like this:
System.out.printf("Temperature 20.5 degree Celsius = %5.1f \n", F);
System.out.printf("Temperature 100.9 degrees Farenheit = %5.1f\n", C);
Reply:You have declared C twice, you don't have to say:
double C;
double C = 1;
Also, you can't have arithmetic on the left side of the assignment. Instead, you will need to do two equations. Since, you have to use the C and F, as inputs for the equations, you need to declare variables to hold the results.
double convertToC;
double convertToF;
convertToC = (F - 32) * 5 / 9;
convertToF = C * 9 / 5 + 32;
Also, the printf is incorrectly formatted.
Something like this should be used:
System.out.printf("Temperature 20.5 degree Celsius = %.1f degree Fahrenheit\n", convertToF);
System.out.printf("Temperature 100.9 degree Farentheit = %.1f degree Celsius\n", convertToC);
This will give you a new line between them and also give you 1 decimal point. The \n is the new line and the %.1f gives you a double formatted to one decimal point.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment