无法找到现有变量的符号

时间:2023-01-25 19:47:35
public static void main(String[] args) {


        int [] array= {4,5,7,1,5,4};
        for(int j=0;j<array.length;j++) {
            int min;
            min=array[0];
            if(array[j]>min) {
                min=array[j];
            }
        }
        System.out.print(min);
    }
}

can you help me why min cannot be found?

你能告诉我为什么找不到我吗?

8 个解决方案

#1


4  

You are attempting to access min from a different scope from where it was declared.

您正在尝试从声明min的不同范围访问min。

You are declaring it inside your for-loop:

您正在声明它在您的for-loop:

for(int j=0; j < array.length; j++) {
    int min;
    min=array[0];
    if(array[j]>min) {
        min=array[j];
    }
} // min can't be used after this.

However, you are using it outside of the loop. int min is only valid within the scope of the for loop block (the } that closes the loop ends the block that min is accessible in).

但是,您正在循环之外使用它。int min仅在for循环块的范围内有效(关闭循环的}终止min可访问的块)。

Move the min declaration outside of the loop and it should work.

将最小声明移到循环之外,它应该工作。

int min = array[0];
for (int i : array) {
    if (i > min) {
        min = i;
    }
}

System.out.println("The min is: " + min);

I made your code a little more syntactically sweet with Java's for-each loop.

我用Java的for-each循环使您的代码更符合语法。


Also, is there a logic error in your code? You call the variable min but you are assigning numbers larger than min to min. Sounds like you're looking for max value, not minimum?

此外,您的代码中是否存在逻辑错误?你称变量为min,但你要分配比min更大的数字到最小值,听起来像是你在寻找最大值,而不是最小值?

#2


2  

You need to declare min outside of the loop (because of scope):

您需要在循环之外声明最小值(因为范围):

int [] array= {4,5,7,1,5,4};
int min = array[0];// initialize min outside the loop.
for(int j=0;j<array.length;j++) {
    //min=array[0]; If you leave this here, you will 'reset' min each time
    if(array[j] < min) { // use "<" so that min will be the minimum
        min=array[j];
    }
}
System.out.print(min);

Also, you should not have min=array[0]; in your loop; this wil cause min to "reset" back to array[0] at each loop iteration. Instead, initialize it outside the loop. Finally, it looks like your code will set min to the largest element in array. To fix, reverse the comparision in the if statement.

同样,不应该有min=array[0];在你的循环;这将导致最小值在每次循环迭代时“重置”到数组[0]。相反,在循环外部初始化它。最后,看起来您的代码将把min设置为数组中最大的元素。要修复,请反转if语句中的比较。

#3


1  

Min is being declared within the for-loop. If you move the declaration outside, it should work fine.

在for循环中声明Min。如果你把声明移到外面,它应该没问题。

For example:

例如:

public static void main(String[] args) {
        int [] array= {4,5,7,1,5,4};
        int min=array[0];
        for(int j=0;j<array.length;j++) {
            if(array[j]>min) {
                min=array[j];
            }
        }
        System.out.print(min);
    }
}

#4


1  

Min is local to the for loop

最小是for循环的局部

    int [] array= {4,5,7,1,5,4};
    int min = array[0];
    for(int j=0;j<array.length;j++) {
        if(array[j]>min) {
            min=array[j];
        }
    }
    System.out.print(min);

#5


0  

min was declared inside the for loop.

在for循环中声明min。

Remove int min; and put the following outside the for loop:

删除int最小;并将以下内容放到for循环之外:

 int min=0;

Since it's a local variable it must be initialized.

因为它是一个局部变量,所以必须对它进行初始化。

Also move min=array[0]; outside the for loop as well.

也移动min = array[0];在for循环之外。

#6


0  

Because the scope of the min variable lasts inside of the for block.

因为最小变量的作用域持续在for块内部。

#7


0  

Shouldn't it be if(array[j]<min) min=array[j]; ?

如果(array[j] )>

#8


0  

Just moving min out of the for loop doesn't help. It will overwrite min everytime. Also move min=array[0]; out of the for loop.

仅仅把最小值移出for循环是没有用的。它每次都会覆盖最小值。也移动min = array[0];在for循环之外。

#1


4  

You are attempting to access min from a different scope from where it was declared.

您正在尝试从声明min的不同范围访问min。

You are declaring it inside your for-loop:

您正在声明它在您的for-loop:

for(int j=0; j < array.length; j++) {
    int min;
    min=array[0];
    if(array[j]>min) {
        min=array[j];
    }
} // min can't be used after this.

However, you are using it outside of the loop. int min is only valid within the scope of the for loop block (the } that closes the loop ends the block that min is accessible in).

但是,您正在循环之外使用它。int min仅在for循环块的范围内有效(关闭循环的}终止min可访问的块)。

Move the min declaration outside of the loop and it should work.

将最小声明移到循环之外,它应该工作。

int min = array[0];
for (int i : array) {
    if (i > min) {
        min = i;
    }
}

System.out.println("The min is: " + min);

I made your code a little more syntactically sweet with Java's for-each loop.

我用Java的for-each循环使您的代码更符合语法。


Also, is there a logic error in your code? You call the variable min but you are assigning numbers larger than min to min. Sounds like you're looking for max value, not minimum?

此外,您的代码中是否存在逻辑错误?你称变量为min,但你要分配比min更大的数字到最小值,听起来像是你在寻找最大值,而不是最小值?

#2


2  

You need to declare min outside of the loop (because of scope):

您需要在循环之外声明最小值(因为范围):

int [] array= {4,5,7,1,5,4};
int min = array[0];// initialize min outside the loop.
for(int j=0;j<array.length;j++) {
    //min=array[0]; If you leave this here, you will 'reset' min each time
    if(array[j] < min) { // use "<" so that min will be the minimum
        min=array[j];
    }
}
System.out.print(min);

Also, you should not have min=array[0]; in your loop; this wil cause min to "reset" back to array[0] at each loop iteration. Instead, initialize it outside the loop. Finally, it looks like your code will set min to the largest element in array. To fix, reverse the comparision in the if statement.

同样,不应该有min=array[0];在你的循环;这将导致最小值在每次循环迭代时“重置”到数组[0]。相反,在循环外部初始化它。最后,看起来您的代码将把min设置为数组中最大的元素。要修复,请反转if语句中的比较。

#3


1  

Min is being declared within the for-loop. If you move the declaration outside, it should work fine.

在for循环中声明Min。如果你把声明移到外面,它应该没问题。

For example:

例如:

public static void main(String[] args) {
        int [] array= {4,5,7,1,5,4};
        int min=array[0];
        for(int j=0;j<array.length;j++) {
            if(array[j]>min) {
                min=array[j];
            }
        }
        System.out.print(min);
    }
}

#4


1  

Min is local to the for loop

最小是for循环的局部

    int [] array= {4,5,7,1,5,4};
    int min = array[0];
    for(int j=0;j<array.length;j++) {
        if(array[j]>min) {
            min=array[j];
        }
    }
    System.out.print(min);

#5


0  

min was declared inside the for loop.

在for循环中声明min。

Remove int min; and put the following outside the for loop:

删除int最小;并将以下内容放到for循环之外:

 int min=0;

Since it's a local variable it must be initialized.

因为它是一个局部变量,所以必须对它进行初始化。

Also move min=array[0]; outside the for loop as well.

也移动min = array[0];在for循环之外。

#6


0  

Because the scope of the min variable lasts inside of the for block.

因为最小变量的作用域持续在for块内部。

#7


0  

Shouldn't it be if(array[j]<min) min=array[j]; ?

如果(array[j] )>

#8


0  

Just moving min out of the for loop doesn't help. It will overwrite min everytime. Also move min=array[0]; out of the for loop.

仅仅把最小值移出for循环是没有用的。它每次都会覆盖最小值。也移动min = array[0];在for循环之外。