Reputation: 91
I need to make a table of Celsius to Fahrenheit conversions. The output should be exactly like this:
-40.0C is -40.0F -30.0C is -22.0F -20.0C is -4.0F -10.0C is 14.0F
-39.0C is -38.2F -29.0C is -20.2F -19.0C is -2.2F -9.0C is 15.8F
-38.0C is -36.4F -28.0C is -18.4F -18.0C is -0.4F -8.0C is 17.6F
-37.0C is -34.6F -27.0C is -16.6F -17.0C is 1.4F -7.0C is 19.4F
-36.0C is -32.8F -26.0C is -14.8F -16.0C is 3.2F -6.0C is 21.2F
-35.0C is -31.0F -25.0C is -13.0F -15.0C is 5.0F -5.0C is 23.0F
-34.0C is -29.2F -24.0C is -11.2F -14.0C is 6.8F -4.0C is 24.8F
-33.0C is -27.4F -23.0C is -9.4F -13.0C is 8.6F -3.0C is 26.6F
-32.0C is -25.6F -22.0C is -7.6F -12.0C is 10.4F -2.0C is 28.4F
-31.0C is -23.8F -21.0C is -5.8F -11.0C is 12.2F -1.0C is 30.2F
The trick is, you can only use one while loop for the whole thing. Also you can't just type everything as it is. I am strugling to figure out a way for how to start a new column each time it goes through the 10 conversions or something of that type, i made a program that works but its lame and kinda cheating:
public class TempConvWorking{
public static void main (String[] args){
double celsius1 = -40.0, celsius2 = -30.0, celsius3 = -20.0, celsius4 = -10.0;
double fahreheit1, fahreheit2, fahreheit3, fahreheit4;
while (celsius4 < 0){
fahreheit1 = (celsius1 * (1.8) + 32.0);
fahreheit2 = (celsius2 * (1.8) + 32.0);
fahreheit3 = (celsius3 * (1.8) + 32.0);
fahreheit4 = (celsius4 * (1.8) + 32.0);
System.out.printf ("%.1fC is %.1fF\t%.1fC is %.1fF\t%.1fC is %.1fF\t%.1fC is %.1fF\t\n",celsius1,fahreheit1,celsius2,fahreheit2,celsius3,fahreheit3,celsius4,fahreheit4);
celsius1 += 1;
celsius2 += 1;
celsius3 += 1;
celsius4 += 1;
}
}
}
so if anyone has suggestions please explain.
Upvotes: 1
Views: 232
Reputation: 22977
I strongly suggest you create a method doing the conversion from Celcius to Fahrenheit and in addition print it. In my example I will use conv(double celcius)
which prints for example "40.0C is -40.0F".
There's a pattern: the ten lines i0 until i9 each contain the conversions for -40 + i
, -30 + i
, -20 + i
and -10 + i
.
Within the while loop, you must call the conv
method four times.
int i = 0;
while (i < 10) {
conv(-40 + i);
conv(-30 + i);
conv(-20 + i);
conv(-10 + i);
System.out.println();
i++;
}
public static void conv(double celcius) {
double fahrenheit = 1.8 * celsius + 32;
System.out.printf("%.1fC is %.1fF\t" celcius, fahrenheit);
}
Upvotes: 2
Reputation: 3020
Well, they're ten degrees off from each other, so you can eliminate several variables to simplify the code somewhat:
public class TempConvWorking {
public static void main (String[] args) {
double celsius = -40.0;
while (celsius < -30.0) {
System.out.printf(
"%.1fC is %.1fF\t%.1fC is %.1fF\t%.1fC is %.1fF\t%.1fC is %.1fF\t\n",
celsius,
1.8*celsius + 32.0,
celsius + 10.0,
1.8*celsius + 50.0,
celsius + 20.0,
1.8*celsius + 68.0,
celsius + 30.0,
1.8*celsius + 86.0);
celsius++;
}
}
}
This code produces the specified output.
Upvotes: 0
Reputation: 1123
Assuming you want no counter variable outside the while loop and don't want to print more than one celsius-fahrenheit pair per iteration:
double celsius = -50.0;
double fahrenheit;
while (celsius < -1){
if(celsius >= -10){
celsius -= 39;
System.out.println();
}
celsius1 += 10;
fahreheit = (celsius * (1.8) + 32.0);
System.out.printf ("%.1fC is %.1fF\t",celsius, fahrenheit);
}
Upvotes: 2