我是第一次在这里的用户,虽然试图找到我的诺贝尔问题的答案有时在过去引导我 .
所以基本上,我有这个代码,我用来找到地球表面的温度,实现了一些我已经提供的论坛 . 我需要找到一个值(epCarbonDi),它会根据大气中的碳含量而变化 . 我每次使用碳量变化时都使用“for”循环让java来完成等式,但是bluej不会编译它,它说变量可能没有被初始化 . 我在“for”循环之前声明了它,但没有为它赋值,因为“for”循环意味着用变量填充变量,然后我需要在循环之外使用它 .
我读到某个地方,如果你只是用0来初始化循环外的变量,它应该以这样的方式工作^^,所以我做了它,它编译并且很好 . 我去执行它,我的所有信息都很好,但是,所有答案都是相同的!
所以我认为“for”循环已经执行了一次并找到了答案,但是之后没有为我需要它的所有其他值完成它?
如果有人有任何建议,我真的需要一些建议 . 我会非常感激 . 我需要能够在“for”循环之外使用epCarbonD变量,并为下一个等式使用循环值/ s!如果有人能指出我正确的方向,那就太棒了 . 我是java的新手,我已经重新阅读了我的所有笔记和教科书,我已经谷歌搜索了它,我找不到任何能使它工作的东西D:
这是我的整个代码
import java.text.DecimalFormat;
import java.math.RoundingMode;
/**
* A program to calculate the surface temperature of Earth
* based on a range of carbon dioxide levels.
*
* @author Lyssa ------ - Student #---------
* @version ----------
*/
public class Stage4
{
public static void main(String[] args)
{
//define constants for calculating emissivity
final double EP_WATER = 0.65; //component of emissivity due to water
final double A = 0.1; //component of emissivity due to carbon dioxide
final double B = 0.06; //component of emissivity due to 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 and celsius.
//declare variables to hold answer values
double epsilon; //emissivity of the planet
double epCarbonDi = 0.0; //component of emissivity due to carbon dioxide
double surfaceTemp;
double surfaceTempCelsius;
//formula to calcluate value of emissivity due to carbon dioxide
for(double carbonDiox = 280.0; carbonDiox <= 400.0; carbonDiox = carbonDiox += 5)
{
epCarbonDi = A + B*Math.log(carbonDiox/PREINDUST_CARBONDI);
}
//formula to calculate emissivity
epsilon = 1.0 - (EP_WATER + epCarbonDi/2);
//write calculation to find surface temperature
surfaceTemp =
Math.pow((SOLAR_CONSTANT/4)*(1.0 - ALBEDO)
/(STEFANB_CONSTANT*epsilon*ORBIT_RAD*ORBIT_RAD), 0.25);
//convert answer from kelvin to celcius
surfaceTempCelsius = surfaceTemp - KELV_CELS_DIFF;
//enable answer to be truncated to 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 a carbon level of " + carbonDiox +
" the surface temperature is: "
+ df.format(surfaceTempCelsius)
+ " \u00b0" + "C");
System.out.print("\n");
}
}
}
这就是我遇到问题的地方:
//declare variables to hold answer values
double epsilon; //emissivity of the planet
double epCarbonDi = 0.0; //component of emissivity due to carbon dioxide
double surfaceTemp;
double surfaceTempCelsius;
//formula to calcluate value of emissivity due to carbon dioxide
for(double carbonDiox = 280.0; carbonDiox <= 400.0; carbonDiox = carbonDiox += 5)
{
epCarbonDi = A + B*Math.log(carbonDiox/PREINDUST_CARBONDI);
}
//formula to calculate emissivity
epsilon = 1.0 - (EP_WATER + epCarbonDi/2);
//write calculation to find surface temperature
surfaceTemp =
Math.pow((SOLAR_CONSTANT/4)*(1.0 - ALBEDO)
/(STEFANB_CONSTANT*epsilon*ORBIT_RAD*ORBIT_RAD), 0.25);