繁体   English   中英

为什么我得到一个变量未声明的错误?

[英]Why am I getting a varriable not declared error?

我不断收到此错误, cannot find symbol - variable minDist即使我知道它已被声明和初始化。 我觉得它直盯着我。 有人知道为什么会这样吗?

与此伴随的还有另一个类文件,但是我不认为其中存在错误。

当我到达minDist时,我在倒数第三行得到它,但是如果我删除了minDist我也会在minCostminMPG上得到它。

public class AnnualFuelUseTester
{
    public static void main(String[] args)
    {
    int sMiles1, sMiles2, sMiles3, sMiles4;
    int eMiles1, eMiles2, eMiles3, eMiles4;
    int[] dist = new int[4];
    double gals1, gals2, gals3, gals4;
    double[] MPG = new double[4];
    double price1, price2, price3, price4;
    double[] cost = new double[4];

    AnnualFuelUse[] fillUps = {new AnnualFuelUse(108438, 108725, 13.9, 2.98),
                               new AnnualFuelUse(108738, 109023, 15.3, 3.02),
                               new AnnualFuelUse(109023, 109232, 10.3, 3.05),
                               new AnnualFuelUse(109564, 109854, 13.1, 3.03)};

    for(int i = 0; i < fillUps.length; i++)
    {
        dist[i] = fillUps[i].calcDistance();
        MPG[i] = fillUps[i].calcMPG();
        cost[i] = fillUps[i].calcCost();
    }
    for (int i = 0; i < dist.length; i++)
    {
        int maxDist = 0;
        int minDist = dist[0];
        if (dist[i] > maxDist)
        {
            maxDist = dist[i];
        }
        if (dist[i] < minDist)
        {
            minDist = dist[i];
        }
    }
    for (int i = 0; i < dist.length; i++)
    {
        double maxMPG = 0;
        double minMPG = MPG[0];
        if (MPG[i] > maxMPG)
        {
            maxMPG = MPG[i];
        }
        if (MPG[i] < minMPG)
        {
            minMPG = MPG[i];
        }
    }
    for (int i = 0; i < dist.length; i++)
    {
        double maxCost = 0;
        double minCost = cost[0];
        if (cost[i] > maxCost)
        {
            maxCost = cost[i];
        }
        if (cost[i] < minCost)
        {
            minCost = dist[i];
        }
    }

    System.out.printf("%15s%15s%15s%15s%15s%15s%15s%15s%15s\n\n"
                       ,"Fill Up", "Days", "Start Miles", "End Miles"
                       ,"Distance", "Gallons Used", "MPG", "Price", "Cost");
    for(int i = 0; i < fillUps.length; i++)
    {
        System.out.printf("%15s%15s%15s%15s%15s%15s%15.2f%15s%15.2f\n"
                          ,(i+1),(int)(1 + i *(i*1.1)), fillUps[i].getmySMiles()
                          ,fillUps[i].getmyEMiles(), dist[i]
                          ,fillUps[i].getmyGals(), MPG[i]
                          ,fillUps[i].getmyPrice(), cost[i]);
    }
    System.out.printf("%10s%10s%30s%30s","Minimum",minDist,minMPG,minCost);
}                        
}

您在for循环内声明了minDist ,因此它仅存在于该循环中,不能在循环外使用。

您在for循环的范围内声明了它。 您需要将int minDist的声明移到该循环之外,移至与执行printf相同的级别。

始终考虑声明变量的范围,因为它决定了变量的可见性。

您在范围内的for块中声明变量。 然后,您尝试从声明它们的范围之外引用这些变量。 那行不通。

public void foo () {

    while (someBool) {

        int someVariable = 0;

        someVariable = 1  // works because using and declaring takes place in the same scope.

    }

 someVariable = 2; // that won't work because variable is not existent in this scope.

}

还要考虑范围可以是分层结构的,这意味着在某些范围内声明的变量在所有嵌套范围内也是可见的:

public void foo () {

    while (someBool) {

        int aVariable = 0;

        if (anotherBool) {

            aVariable = 1; // works because this scope is a nested scope inside the scope where the variable has been declared.

        }
    }
}

您会发现很多有关众所周知的作用域概念的信息,这些概念不仅在C#中使用,而且在大多数编程语言中都使用。

开始研究的重点可能是MSDN文档:

http://msdn.microsoft.com/zh-CN/library/aa691132(v=vs.71).aspx

您在循环内部声明了minDist变量,因此该变量的范围限于特定的for循环。

因此您无法在外部访问该变量。

基本上,因为你说

int minDist = dist[0];

在您的for循环中,它仅存在于您的循环中。 例如,

for(int i = 0; i < 10; i++) {
    int x = 0;
}
System.out.println(x);

将返回错误,因为x不在该循环之外。 它被称为范围,基本上是不同级别的隐形。 就像电影的开始一样思考-第二级梦想的人知道第一级的梦想是什么,但是第一级看不到第二级的梦想。 因此:

int x = 5;
for(int i = 0; i < 1; i++) {
   x = 3; 
   int y = 10;
}
System.out.println(x);
System.out.println(y);

将成功打印出3,但在尝试打印y时崩溃,因为简单地说,他们在for循环之外看不到y。

要解决您的问题:只需在循环外(靠近开始处的某个位置)声明minDist,它就可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM