I need to create a program that will output a formatted two column list converting Celsius to Fahrenheit ending at the temperature 40 degrees from the starting temperature, which is entered by the user. It's supposed to look like this: (except it counts up from whatever the user enters as Celsius)
I've been working at this for hours and I have no idea how to fix it. Not only is the Celsius starting at 1 (which I think is that (cel=1;) bit), but I have no idea why Fahrenheit is not calculating correctly.
Here's my current source:
import java.util.Scanner;
public class TempTable {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
float cel;
double column;
double Fahrenheit;
final double C_2_F = (9.0 / 5.0);
System.out.println("Enter your city's temperature in Celsius.");
cel = kb.nextFloat();
System.out.println("Here is the conversion from " + cel);
System.out.printf("%2s%12s%n", "Celcius", "Fahrenheit");
for (cel = 1; cel <= 10; cel++) {
column = cel;
for (Fahrenheit = 1; Fahrenheit <= 10; Fahrenheit++) {
Fahrenheit = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n", cel, Fahrenheit);
}
}
kb.close();
}
}
Fix your loop to not reset 'Fahrenheit' each time through.
for ( Fahrenheit = 1; Fahrenheit <=10; Fahrenheit++) {
Fahrenheit = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n",cel,Fahrenheit);
}
To
for ( Fahrenheit = 1; Fahrenheit <=10; Fahrenheit++) {
double f = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n",cel,f);
}
Would be one way. Also rename 'Fahrenheit' to 'fahrenheit'
Actually, that's only one issue. That's just going to print out the same value 10 times since 'cel' and 'C_2_F' don't change during the loop...
Related
My program so far:
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args)
{
int count = 20;
double fahrenheit;
double celsius;
String input;
// Scanner object for keyboard input.
Scanner kb = new Scanner(System.in);
// Get the information.
System.out.print("Enter your starting temperature in Fahrenheit: ");
fahrenheit = kb.nextDouble();
// Display the results in a table.
System.out.println("Fahrenheit Celsius");
System.out.println("-----------------------");
for (fahrenheit = fahrenheit; fahrenheit <= count; fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
}
}
Now what I need it do is to produce a table of 20 temperature conversions from Fahrenheit to Celsius.
If the User entered 0, here is a sample of the first 3 lines of what your output could look like:
FAHRENHEIT: 0 CELSIUS: -17.78
FAHRENHEIT: 5 CELSIUS: -15.00
FAHRENHEIT: 10 CELSIUS: -12.22
etc...
The problem is that it won't loop the correct amount of times if the input is more than 20.
Here is an example of what would happen if the user input 5:
Output:
Enter your starting temperature in Fahrenheit: 5
Fahrenheit Celsius
5.00 -15.00
10.00 -12.22
15.00 -9.44
20.00 -6.67
And then that is where the output ends.
Does anyone know how I can make it so that the program will display 20 F to C equivalents no matter what the user inputs?
Your for loop is all messed up: for (fahrenheit = fahrenheit; fahrenheit <= count; fahrenheit += 5) it is comparing tempratures to a count (fahrenheit <= count).
If you always want 20 steps then something like:
for (int i = 0; i < count; i++) {
...
fahrenheit += 5;
}
would do it.
Option 1, add a counter and use it.
for (int i = 0; i < count; i++, fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
Option 2, decrement count (and test against zero) while you loop.
for (; count > 0; count--, fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
What you need to do is keep a separate index that starts at 0 and goes up to 20. That means that there would be 2 variables being changed each time, index and fahrenheit, and the for-loop would look as follows:
int index = 0
for (fahrenheit = fahrenheit; index < count; fahrenheit += 5) {
...
index++;
}
I am very new to Java and I have to write a program that takes from -40 to 120 Fahrenheit and convert it to Celsius and then display them in two columns with increments of 5 and accurate to one decimal place. I have most of the code finished and I have the right output for the Fahrenheit but I can't figure out how to get the Celsius to print accurate to one decimal place. Anything would be helpful!
package 3;
public class prog2 {
public static void main(String[] args) {
System.out.println("Fahrenheit to Celsius converter from -40F below to 120F");
for(double temp = -40.0; temp <= 120; temp += 5)
{
System.out.printf("%10.1f", temp);
double sum = (temp -32) * (5.0/9.0);
System.out.printf("%5d",(int) sum );
System.out.println();
}
}
}
Change the printing of sum to this:
System.out.printf(" %10.1f", sum );
Formatting is same as for the first column and don't cast the calculated value to int.
you can use DecimalFormat class by sending in the constructor a formating string. In your exemple '#' means one digits and the '.' means decimal point.
check this link for more https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
you can do something like that :
public static void main(String[] args) {
System.out.println("Fahrenheit to Celsius converter from -40F below to 120F");
DecimalFormat df = new DecimalFormat("###.#");
for (double temp = -40.0; temp <= 120; temp += 5) {
System.out.printf("%sC°", df.format(temp));
double sum = (temp - 32) * (5.0 / 9.0);
System.out.printf("= %sF°", df.format(sum));
System.out.println();
}
}
So I am having a problem figuring out this celsius to fahrenheit with methods, Ive done tried to figure this problem out myself for days and ive thrown in the towel and need help.
Here is the code:
public class ctof {
public statice void main(string[] args) {
for (double i = 40; i >= 30; i--) {
double j = 130;
double x = celsiusToFahrenheit(0)
double y = fahrenheitToCelsius(0)
System.out.println(i + " " + (x + i) + " | " + (j + i) + " " + (y + i)
}
public static double celsiusToFahrenheit(double celsius) {
double fahrenheit = (5.0 / 9) * celsius + 32;
return fahrenheit;
}
public static double fahrenheitToCelsius(double fahrenheit) {
double celsius = (9.0 / 5) * fahrenheit - 32;
return celsius;
}
}
Note that for the numbers to decrement in the loop 1234 exe I have to group them in parentsies (lettet + letter and if I dont group them or attach them to an I they dont increment but stay stagnant.
Note 2: When I go to increment them by 10 they dont increment but only add 10 to where they start so instead of starting with 70 they do 60 or 70, not 70, 60 50... the way I need them to
When I do them in a non method everything is fine.
Can somebody tell me what im doing wrong and how to correct it, thank you.
My task is to write a program that displays a table of 20 temperature conversions from Fahrenheit to Celcius and to increment he value in 3 degrees.
This is what I made
public class FahrenheitToCelsius {
public static void main(String[] args) {
for(int f = 20; f < 78; f = f+3) {
System.out.println(f + " degrees Fahrenheit is " + (5/9) * (f-32) + " degrees celsius ");
}
}
}
Yet when I print it is keeps saying the degrees is 0 for all of them, do I have my order of operations wrong or something?
You are working with int so (5/9) * (f-32) evaluates to 0 * (f-32) because integer maths stays integer and 5/9 as an integer is zero. 5.0/9.0 = 0.55555555 which, when converted to an int becomes 0.
Switch to using float.
for (float f = 20; f < 78; f = f + 3) {
System.out.println(f + " degrees Fahrenheit is " + (5.0 / 9.0) * (f - 32) + " degrees celsius ");
}
I need to convert Fahrenheit to Celsius in a Java program with methods, a for loop statement, and no user input. It is supposed to display Fahrenheit temperatures 0 through 20 and it's Celsius conversions. Any solutions?
import java.util.Scanner;
public class celsiusTempTable
{
public static void main(String[] args)
{
System.out.println("Fahrenheit to Celsius Conversion Table");
double tempC = celsiusConversion(tempC);
int tempF = fahrenheit(tempF);
displayData(tempF, tempC);
}
public static int fahrenheit(int F)
{
for(F = 0; F <= 20; F++)
{
return F;
}
}
public static double celsiusConversion(double C)
{
Scanner input = new Scanner(System.in);
for(int F = 0; F <= 20; F++)
{
C = (5.0/9.0) * (F - 32);
return C;
}
}
public static void displayData(int F, double C)
{
for(F = 0; F <= 20; F++)
{
System.out.println("\nThe temperature in Fahrenheit is: " + F);
System.out.println("The temperature in Celsius is: " + C);
}
}
}
I'll only give hints.
Without user input. So forget about using a Scanner and System.in.
You need to understand what method arguments and method return values are. Arguments are usually the inputs of a method. And the return value is the output of the method. You're being asked to translate a temperature in fahrenheit degrees to a temperature in celsius degrees. This is a perfect situation where a method is useful. The input of the translation method is thus a unique integer value (the temperature in fahrenheit degrees), and the output is another single integer value (the temperature in celsius degrees).
You must do that 21 times. Once with 0 as input, once with 1 as input, etc. until 20. This means you need a loop, and that at each iteration, you will translate the current temperature (0, 1, 2, etc.) into celsius degrees by calling the translation method, and print the result. Read your text book about for loops. This part should be in the main method.
A quick search and have found the following:
Error: celsius cannot be resolved to a variable
Java Fahrenheit to Celsius loop (with methods)
Fahrenheit to Celsius conversion
Celsius/Fahrenheit conversion error