gpt4 book ai didi

java - 在 for 循环内部和循环外部使用变量

转载 作者:行者123 更新时间:2023-12-01 14:33:43 26 4
gpt4 key购买 nike

我是这里的第一次用户,尽管过去有时试图找到我的傻瓜问题的答案有时会导致我来到这里。

所以基本上,我有这段代码用于查找地球的表面温度,实现了我提供的一些论坛。我需要找到一个值(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);

最佳答案

在循环中,epCarbon 值每次都会被替换,因此每次循环完成后,epCarbon 中的值将是 A + B*Math.log(400/PREINDUST_CARBONDI); 的结果;它相当于我上面提到的单个语句,因此 for 循环没有任何意义。告诉我您在 epCarbonDi 循环中到底想要什么,对于每个迭代结果,您希望它添加到先前的结果中,在这种情况下将代码更改为以下

for(double carbonDiox = 280.0; carbonDiox <= 400.0; carbonDiox = carbonDiox += 5)
{
epCarbonDi = epCarbonDi+(A + B*Math.log(carbonDiox/PREINDUST_CARBONDI));
}

或者如果你想要整个事情直到每次迭代的system.out,那么将整个事情放入1个循环中

//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);
System.out.print("For a carbon level of " + carbonDiox +
" the surface temperature is: "
+ df.format(surfaceTempCelsius)
+ " \u00b0" + "C");
System.out.print("\n");
}

如果这不是您想要的,那么请告诉我您想要从该循环中得到什么作为 epCarbonDi 的值,我会帮助您

关于java - 在 for 循环内部和循环外部使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16661855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com