在循环外部的for循环中使用变量

时间:2013-05-21 03:55:24

标签: java variables for-loop

我是第一次在这里的用户,虽然试图找到我的诺贝尔问题的答案有时在过去引导我。

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

4 个答案:

答案 0 :(得分:1)

在你的循环中,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然后把整个事情放在一个循环中

//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的价值我会帮助你

答案 1 :(得分:0)

我看到的一些问题

  • 你在循环中分配给epCarbonDi但是编译器无法确定循环总是被执行的事实,第一次迭代的条件可能是假的,因此事后可以使用epCarbonDi未初始化。
  • 您使用carbonDiox = carbonDiox += 5作为增量条件,但它等同于carbonDiox += 5
  • 在你的循环中你没有累积任何值,只是分配给它(epCarbonDi = ..),从而覆盖前一次迭代,它是预期的行为还是什么?

答案 2 :(得分:0)

@ honeyb_93如果覆盖epCarbonDi不是所需的行为,那么您应该使用一个存储epCarbonDi的所有值的数组。

        double epsilon;                         //emissivity of the planet
        double epCarbonDi[] = new double[ /*NO. OF VALUES TO BE STORED*/ ];                //component of emissivity due to carbon dioxide
        double surfaceTemp;
        double surfaceTempCelsius;

        //formula to calcluate value of emissivity due to carbon dioxide
        int i = 0;
        for(double carbonDiox = 280.0; carbonDiox <= 400.0; carbonDiox = carbonDiox += 5)
        {
            epCarbonDi[i] = A + B*Math.log(carbonDiox/PREINDUST_CARBONDI);
            i = i+1;
        }

检查一下。

答案 3 :(得分:0)

您使用的变量是double。每次执行for循环时,它都会为所需变量赋值。但是在每次迭代时,都会重写该值。即在循环的第一次运行中。假设变量被赋值为1.在第二次运行中,您将另一个值重新分配给同一个变量,例如2,依此类推。因此变量将始终具有从for循环的最后一次运行计算的值。

如果要使用for循环中计算的每个值,请将其存储在数组或列表中,然后根据需要使用它们。

相关问题