java - use a variable from inside a for loop, outside of the loop -
i'm first time user here, though trying find answers nooby question have led me here in past.
so basically, have code i'm using find surface temperature of planet earth, implementing few forumlas i've been provided with. need find value (epcarbondi) changes depending on carbon levels in atmosphere. used "for" loop java equation each time amount of carbon changes, bluej wouldnt compile it, said variable might not have been initialised. had declared before "for" loop not assigned value "for" loop meant there fill variable value, need use outside loop afterwards.
i read somewhere if initialise variable outside loop 0, should work way^^, did it, , compiles , great. go execute it, , lines of info come out fine lo , behold, answers same!!
so gather "for" loop has executed once , found answer not done other values need for?
i really need advice if has any..i'd super grateful. need able use epcarbondi variable outside "for" loop loop value/s next equation! if point me in right direction fantastic. i'm new java, i've re-read notes , textbook have , i've google searched , cant find makes work d:
this whole code
import java.text.decimalformat; import java.math.roundingmode; /** * program calculate surface temperature of earth * based on range of carbon dioxide levels. * * @author lyssa ------ - student #--------- * @version ---------- */ public class stage4 { public static void main(string[] args) { //define constants calculating emissivity final double ep_water = 0.65; //component of emissivity due water final double = 0.1; //component of emissivity due carbon dioxide final double b = 0.06; //component of emissivity due carbon dioxide final double preindust_carbondi = 280; //pre-industrial level of carbon dioxide in earth's atmosphere //define surface temperature constants final double solar_constant = 1367; final double albedo = 0.3; final double stefanb_constant = 5.67e-8; final double orbit_rad = 1.0; //the orbital radius of earth. final double kelv_cels_diff = 273.15; //the difference between kelvin , celsius. //declare variables hold answer values double epsilon; //emissivity of planet double epcarbondi = 0.0; //component of emissivity due carbon dioxide double surfacetemp; double surfacetempcelsius; //formula calcluate value of emissivity due carbon dioxide for(double carbondiox = 280.0; carbondiox <= 400.0; carbondiox = carbondiox += 5) { epcarbondi = + b*math.log(carbondiox/preindust_carbondi); } //formula calculate emissivity epsilon = 1.0 - (ep_water + epcarbondi/2); //write calculation find surface temperature surfacetemp = math.pow((solar_constant/4)*(1.0 - albedo) /(stefanb_constant*epsilon*orbit_rad*orbit_rad), 0.25); //convert answer kelvin celcius surfacetempcelsius = surfacetemp - kelv_cels_diff; //enable answer truncated 2 decimal places decimalformat df = new decimalformat("####0.00"); df.setroundingmode(roundingmode.floor); for(double carbondiox = 280.0; carbondiox <= 400.0; carbondiox = carbondiox += 5) { system.out.print("for carbon level of " + carbondiox + " surface temperature is: " + df.format(surfacetempcelsius) + " \u00b0" + "c"); system.out.print("\n"); } } }
and im having issues:
//declare variables hold answer values double epsilon; //emissivity of planet double epcarbondi = 0.0; //component of emissivity due carbon dioxide double surfacetemp; double surfacetempcelsius; //formula calcluate value of emissivity due carbon dioxide for(double carbondiox = 280.0; carbondiox <= 400.0; carbondiox = carbondiox += 5) { epcarbondi = + b*math.log(carbondiox/preindust_carbondi); } //formula calculate emissivity epsilon = 1.0 - (ep_water + epcarbondi/2); //write calculation find surface temperature surfacetemp = math.pow((solar_constant/4)*(1.0 - albedo) /(stefanb_constant*epsilon*orbit_rad*orbit_rad), 0.25);
in loop evertime epcarbon value replaced ever time once loop completed value in epcarbon reult of + b*math.log(400/preindust_carbondi); equivalent of single statement mentioned above loop doesnt make sense. tell me want inside loop epcarbondi every iteration result want add previous result in case change code following
for(double carbondiox = 280.0; carbondiox <= 400.0; carbondiox = carbondiox += 5) { epcarbondi = epcarbondi+(a + b*math.log(carbondiox/preindust_carbondi)); }
or if want whole thing till system.out each iteration put whole thing in 1 loop
//formula calcluate value of emissivity due carbon dioxide for(double carbondiox = 280.0; carbondiox <= 400.0; carbondiox = carbondiox += 5) { epcarbondi = + b*math.log(carbondiox/preindust_carbondi); //formula calculate emissivity epsilon = 1.0 - (ep_water + epcarbondi/2); //write calculation find surface temperature surfacetemp = math.pow((solar_constant/4)*(1.0 - albedo) /(stefanb_constant*epsilon*orbit_rad*orbit_rad), 0.25); //convert answer kelvin celcius surfacetempcelsius = surfacetemp - kelv_cels_diff; //enable answer truncated 2 decimal places decimalformat df = new decimalformat("####0.00"); df.setroundingmode(roundingmode.floor); system.out.print("for carbon level of " + carbondiox + " surface temperature is: " + df.format(surfacetempcelsius) + " \u00b0" + "c"); system.out.print("\n"); }
if not want tell me want out of loop value epcarbondi you
Comments
Post a Comment