Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes program with the dreaded for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "program with the dreaded for loop" Watch "program with the dreaded for loop" New topic

program with the dreaded for loop

MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
I am writing a program to convert Celsius to Fahrenheit using a for loop. Since I have such problems with for loops and LOGIC in general, I need help! Here is a sample of what I've written so far. There is an output in the textbook that needs to be mirrored in the program that decrements the C temp from 40 to 31 and prints out the F equivalent. Please give me some clues and pointers!!!

package Chapter4;
import Chapter2.MyInput;
public class ConvertTemp
//Main method
public static void main(String[] args)
System.out.println("Cels.Temp. Fahr.Temp.");
//Class containing C to F conversion method
class CelsToFahr{
//Find the Fahrenheit equivalent for Celsius value
public static double CelsToFahr(double Cels)
double Fahr = 32.00;
// Fahr= ((9/5)*Cels)+32;
// for(Cels = 0; Cels < Fahr; Cels++)
Fahr = ((9.0/5.0)*Cels)+32;
Cels = (Fahr - 32.0)*(5.0/9.0);
//if(Cels == 40.00)
// Fahr= ((9/5)*Cels)+32;
while(Cels == 40.00);
// return Fahr;

Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
Dear MaryEllen,
Check out the tutorial at for help with a for loop. Here's a start:
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113
Hi there,
What I would recommend is to write an algorithm before you code.
in this case it would be something like that:
get the value in celsius (can be something like 'double cels = 32.0' or user input)
convert it to farhaneit (i.e. apply the calculation)
return the result (System.out)
no for loops are needed to solve your problem.
hope it helps.
MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
thank you for you reply....however, the assignment is a PAIN in that it requires the use of a for loop. I can usually solve them without, but can NEVER solve them with!!! Thanks, keep helping!!!
Mary Ellen
Ambi Raj

Joined: Feb 12, 2001
Posts: 1
I agree with Matt that it is infinitely better to have an algorithm beforehand. As I see it, you need to use the for loop as part of your exercise. So your psuedocode would be something like this.
int celsius;
double result= celsiusToFaren(celsius);
Obviously, you will need to write the definitions of the
methods for "celciusToFaren" and "display".
Hope this helps
I agree. Here's the link:
subject: program with the dreaded for loop
It's not a secret anymore!