Hello, extreme Java idiot here.
For lab, we had to have the program output to the console screen the conversion of 98.6 degrees Fahrenheit to celsius. I did that.
Then we had to have it print the conversion of Fahrenheit 0.0, 5.0, 10.0.... 40.0 using a for loop. Below, I think I did it.
public class Lab3 { public static void main (String[] args){ float fahrenheit=98.6f, celsius; celsius=(5.0f/9)*(fahrenheit-32); System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius."); System.out.println(); for (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){ celsius=(5.0f/9)*(fahrenheit-32); if (fahrenheit%5==0) System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius."); } } }
After that, we had to use a while loop instead. Here is where i'm confused:
With while loop replacing for loop:
All I did was replace the if statement with a while one.
public class Lab3 { public static void main (String[] args){ float fahrenheit=98.6f, celsius; celsius=(5.0f/9)*(fahrenheit-32); System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius."); System.out.println(); for (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){ celsius=(5.0f/9)*(fahrenheit-32); while (fahrenheit%5==0) System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius."); } } }
However, my program begins to run infinitely and does not stop when I do this.
But actually what the assignment says is that I have to replace the for loop with a while loop? So am I supposed to change the 'for
to a 'while?'
Thanks in advance for having patience with me.