我是这里的第一个用户,尽管过去有时试图找到我的问题的答案有时会导致我来到这里。
所以基本上,我有这个代码用来查找地球的表面温度,实现了我提供的一些论坛。我需要找到一个值(epCarbonDi),它会根据大气中的碳含量而变化。每次碳含量发生变化时,我使用“for”循环让 java 执行方程式,但 bluej 不会编译它,它说变量可能尚未初始化。我在“for”循环之前声明了它,但没有给它赋值,因为“for”循环的目的是用一个值填充变量,然后我需要在循环之外使用它。
我在某处读到,如果你只是用 0 初始化循环外的变量,它应该以这种方式工作^^,所以我做到了,它可以编译,而且很棒。我去执行它,我所有的信息都很好,但是你瞧,所有的答案都是一样的!!
所以我收集到“for”循环已经执行了一次并找到了答案,但是没有为我需要它执行的所有其他值执行它?
如果有人有任何建议,我真的很需要一些建议。我将非常感激。我需要能够在“for”循环之外使用 epCarbonDi 变量和下一个方程的循环值!如果有人能指出我正确的方向,那就太好了。我对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);