i have been tasked creating conversion table, , cannot figure out why output on 1 side repeating other finishes loop. can help? below code:
public class conversion { public static double celsiustofahrenheit(double celsius) { // make conversion double f = (celsius * 9 / 5) + 32; return f; } public static double fahrenheittocelsius(double fahrenheit) { // make conversion double c = (fahrenheit - 32.0) * 5 / 9.0; return c; } public static void main(string args[]) { double c; double f; //display table system.out.println("celsius \t fahrenheit \t | \tfahrenheit \t celsius"); //when = 40, if greater than, or equal 31, decriment until false (double = 40; >= 31; i--) { c = i; //when j = 120, if j greater than, or equal 30, decriment 10 until false (double j = 120; j >= 30; j -= 10) { f = j; //show result system.out.println(c + "\t\t " + (math.round(celsiustofahrenheit(c) * 10.0) / 10.0) + "\t\t |\t" + f + "\t\t" + (math.round(fahrenheittocelsius(f) * 100.0) / 100.0)); } } } }
this due nested loop you've introduced.
for (double = 40; >= 31; i--) { c = i; //when j = 120, if j greater than, or equal 30, decriment 10 until false (double j = 120; j >= 30; j -= 10) { f = j; //show result system.out.println(c + "\t\t " + (math.round(celsiustofahrenheit(c) * 10.0) / 10.0) + "\t\t |\t" + f + "\t\t" + (math.round(fahrenheittocelsius(f) * 100.0) / 100.0)); } } think of double loop hands of watch face. inner loop moves faster outer loop (like minute hand moves faster hour hand). means every iteration in celsius loop, have ten iterations in fahrenheit loop, if looking @ watch face (we'd have 60 minutes every 1 hour in scenario).
as hint, can declare multiple variables inside of loop iterate on. you're going want adjust bounds of loop condition results want, start.
//when = 40, if greater than, or equal 31, decriment until false //when j = 120, if j greater than, or equal 30, decriment 10 until false (double = 40, j = 120; >= 31 && j >= 30; i--, j -= 10) { c = i; f = j; //show result system.out.println(c + "\t\t " + (math.round(celsiustofahrenheit(c) * 10.0) / 10.0) + "\t\t |\t" + f + "\t\t" + (math.round(fahrenheittocelsius(f) * 100.0) / 100.0)); }
Comments
Post a Comment