Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重复局部变量n_Java - Fatal编程技术网

Java 重复局部变量n

Java 重复局部变量n,java,Java,我一直在尝试用java解决一个问题,并试图寻找答案。 我找不到任何东西,除了我可能已经声明了一个变量两次,我不知道在哪里 我试图获得用户输入,一个整数“n”,作为瓶子的起始数量。 请帮助并告诉我如何更改和修复此问题 以下是我的代码部分: public class BottlesOfBeer { private static Scanner bottles; public static void number(int n) { bottles = new Scan

我一直在尝试用java解决一个问题,并试图寻找答案。 我找不到任何东西,除了我可能已经声明了一个变量两次,我不知道在哪里

我试图获得用户输入,一个整数“n”,作为瓶子的起始数量。 请帮助并告诉我如何更改和修复此问题

以下是我的代码部分:

public class BottlesOfBeer {

    private static Scanner bottles;
    public static void number(int n) {
        bottles = new Scanner(System. in );
        bottles.useDelimiter("\n");

        System.out.println("Enter the starting number of " + "bottles in the song " + "'99 Bottles of Beer on the Wall':");
        int n = bottles.nextInt();

        if (n > 1) {
            System.out.print(n + " bottles of beer on the wall, " + n + " bottles of beer, ya' take one down, " +
                "ya' pass it around, ");
            n = n - 1;
            System.out.println(n + " bottles of beer on the wall.");
            number(n);
        } else {

            if (n == 1) {
                System.out.print(n + " bottle of beer on the wall, " + n + " bottle of beer, ya' take one down, " +
                    "ya' pass it around, ");
                n = n - 1;
                System.out.println(n + " bottles of beer on the wall.");
                number(n);
            } else {
                System.out.println("No more bottles of beer on the wall, " +
                    "no bottles of beer, ya' can't take one down, " + "ya' can't pass it around, 'cause there are" + " no more bottles of beer on the wall!");
            }

        }
    }

number
方法的签名中既有参数
n
,也有方法中定义的变量
n
。由于编译器无法区分这两者,因此必须重命名其中一个

public static void number(int n) { // first n
    bottles = new Scanner(System.in);
    bottles.useDelimiter("\n");

        System.out.println("Enter the starting number of " 
                + "bottles in the song "
                + "'99 Bottles of Beer on the Wall':");
        int n = bottles.nextInt(); // second n

不允许在同一范围内声明多个同名变量。在代码中,您已经在名为number的方法中声明了两次名为'n'的变量。因为这两个变量在同一个方法作用域中,所以当您试图引用变量“n”时,编译器无法决定引用哪个变量

请注意,可以声明多个具有相同名称的变量,只要它们位于不同的作用域中。例如,以下是允许的

public class VariableScope {
    int scopedVar = 10; // First declaration
    public static void main(String[] args) {
            int scopedVar = 200; // Second declaration. 
                                 // This is perfectly valid. But it overrides the first declaration.

    }
}

谢谢,我没有意识到我已经在参数中声明了n。我删除了n的第二个声明,它解决了我的问题。