# Whats wrong with this for loop?

• May 26th, 2013, 02:02 AM
dx2731
Whats wrong with this for loop?
I have this code here:
Code java:

``` for(C0 = 280.0; C0 <= 400.0; C0 = C0 += 5) { Carbondep = A + B*Math.log(C0/PRE_CARBON);   //formula to calculate emissivity earthEmissivityep = 1.0 - (H2O_EMISSIVITY + Carbondep/2);   //write calculation to find surface temperature eSurfacetemp = Math.pow((SOLAR_CONSTANT/4)*(1.0 - EARTH_ALBEDO) /(STEFAN_B_CON*Carbondep*RADIUS*RADIUS), 0.25);   //convert answer from kelvin to celcius surfaceTempCel = eSurfacetemp - KELVIN_TEMP;   //enable answer to be truncated to 2 decimal places DecimalFormat df = new DecimalFormat("####0.00"); df.setRoundingMode(RoundingMode.FLOOR); System.out.print("For a carbon level of " + C0 + " the surface temperature is: " + df.format(surfaceTempCel) + "C"); System.out.print("\n"); }```

It compiles fine, but when I run it, all the printed out answers are the same. I've tried everything to get the code to print out properly, but all it does is repeat the same answer over again. What is wrong with this. Im guessing its a simple fix, but I cant even find out what is wrong.
• May 26th, 2013, 04:19 AM
angstrem
Re: Whats wrong with this for loop?
One guy posted similar question with similar assignment yesterday.

Now remember: int/int=int, not double. For example C0/PRE_CARBON is always an integer, not double.
• May 26th, 2013, 04:23 AM
dx2731
Re: Whats wrong with this for loop?
I figured it out now. I guess our lecturers dont make themselves very clear :/
• May 26th, 2013, 04:27 AM
angstrem
Re: Whats wrong with this for loop?
That's why self-education is better :)
• May 26th, 2013, 06:04 AM
dx2731
Re: Whats wrong with this for loop?
Okay now I have this code:
Code java:

```public class climate { public static void main(String[] args) { //constants for calculating emissivity final double SOLAR_CONSTANT = 1367; final double ALBEDO = 0.3; final double STEFAN_B_CONSTANT = 5.67E-8; final double RADIUS = 1.0; final double KELVIN_TEMP = 273.15; final double H2O_EMISSIVITY = 0.65; final double A = 0.1; final double B = 0.06; final double PRE_CARBON = 280;   double carbondiep = 0.0; double emissivity; double earthsurfaceTemp; double surfaceTempCel;     for(double C0 = 280.0; C0 <= 400.0; C0 = C0 += 5) { carbondiep = A + B * Math.log(C0/PRE_CARBON);   //Calculate emissivity emissivity = 1.0 - (H2O_EMISSIVITY + carbondiep)/2;   //Surface temperature formula earthsurfaceTemp = Math.pow((SOLAR_CONSTANT/4)*(1.0 - ALBEDO)/(STEFAN_B_CONSTANT*emissivity*RADIUS*RADIUS), 0.25) - KELVIN_TEMP;   //enable answer to be truncated to 2 decimal places DecimalFormat df = new DecimalFormat("####0.00"); df.setRoundingMode(RoundingMode.FLOOR); System.out.print("For a carbon level of " + C0 + " the surface temperature is: " + df.format(earthsurfaceTemp) + "C"); System.out.print("\n"); } } }```

I need to put a helper method in here... basically I want to, to learn more skills. But I dont know where to put one. Please only answers, im due to hand this in a few hours. Then explain.
• May 26th, 2013, 06:14 AM
angstrem
Re: Whats wrong with this for loop?
You can, for instance, make a method with the whole formula. So, that your loop would look like as follows:
Code Java:

```for(double c = 280; c <= 400; c += 5) { String earthsurfTemp = computeWithAhelper(c); System.out.println("C = " + c + "; temp = " + earthsurfTemp); }```
Here, computeWithAhelper() is a helper method to simplify the code.
• May 26th, 2013, 06:52 AM
dx2731
Re: Whats wrong with this for loop?
Sorry, I still dont understand. how does it simplify the code? and how is any calculations associated with the helper?

--- Update ---

```public static void main(String[] args) { for(double c = 280; c <= 400; c += 5) { String earthsurfTemp = computeWithAhelper(c); System.out.println("C = " + c + "; temp = " + earthsurfTemp); } }```