java - 为什么当我尝试更改此 case 语句中的变量时,它不会更改该循环之外的变量

标签 java while-loop scope initialization

为什么当我启动(?,我刚刚声明了变量的名称,我不确定这叫什么太累了不知道)案例之外的变量时会发生这种情况,为什么它不只是改变多变的。我并不是在举个例子。为什么我一定要举个例子呢?这似乎是一件我应该能够做的简单的事情。我如何在不进行任何变量静态的情况下解决这个问题。

我正在下面编写一些代码,我想我会把它们全部保留,但看看第三种情况,当我尝试运行它时,我在第二种情况中更改的内容不会影响名称等变量[]和amouintOfNameds

代码:

package comp1skeleton2015;

class InputOutput
{


    public static void main (String args[])
    {
        AQAConsole2015 console = new AQAConsole2015();
        AQAWriteTextFile2015 writeAQA = new AQAWriteTextFile2015();
        AQAReadTextFile2015 readAQA = new AQAReadTextFile2015();
        String doProgram = "yes";

        while(doProgram.equals("yes"))
        {
            String[] names;
            String fileOutput = "names.txt";
            int amountOfNames = 0;
            int amountOfNames2 = 0;
            int x = 0;
            names = new String[1];
            names[0] = "";

            console.println("Would you like to: ");
            console.println("1 - Write names to a file ");
            console.println("2 - Read names from a file ");
            console.println("3 - Display these names ");
            int option = console.readInteger("Please enter the corresponding number for the option: ");

            switch (option)
            {   
                case 1:
                {
                    int numOfNames = console.readInteger("Please enter how many names you would like to enter ");
                    names = new String[numOfNames];
                    writeAQA.openFile(fileOutput);
                    for (int i = 0; i < numOfNames; i++)
                    {
                        String temp = console.readLine("Enter name " + i + ".");
                        writeAQA.writeToTextFile(temp);
                    }
                    writeAQA.closeFile();
                }
                break;
                case 2:
                {
                    readAQA.openTextFile(fileOutput);
                    boolean continueLoop = true;

                    while(readAQA.readLine() != null)
                    {
                        amountOfNames = amountOfNames + 1;
                    }

                    console.println (amountOfNames);
                    readAQA.closeFile();
                    readAQA.openTextFile(fileOutput);
                    names = new String[amountOfNames];

                    for(int i = 0; i < amountOfNames; i++)
                    {
                        names[i] = readAQA.readLine();
                    }
                    amountOfNames2 = amountOfNames;
                    readAQA.closeFile();
                }
                break;
                case 3:
                {
                    console.println (amountOfNames2);
                    console.println(names[1]);
                    for(int y = 0; y < amountOfNames; y++)
                    {
                        console.println(y + ": " + names[y]);
                    }
                }
                break;
                default:
                {   
                    console.println("You have entered an incorrect option, please try again");
                }
                break;

            }
            //doProgram = console.readLine("yes to continue");
        }
    }
}

非常感谢您的阅读,我希望能帮助您度过难关,再见,谢谢。

最佳答案

您有一个 while 循环,并且在循环的每次传递中都会获取用户输入。因此,用户在一次循环中输入 2,然后您读取文件并填充变量。在循环的下一轮中,用户输入 3,并且您尝试打印这些变量。现在 while 循环的工作方式是一遍又一遍地执行循环内的代码段。在 case 2 中填充变量后,循环从顶部开始,查找 namesamountOfNames 等的变量声明,并进行初始化将它们设置为空、0 等。

如果您希望在一次循环中对这些变量所做的更改在下一次循环中可用,则需要在 while 循环之外声明它们。

用变量作用域解释上述内容:虽然您已在 case 语句之外声明了变量,但它们仍然位于 while内部> 循环,这将它们的范围限制为 while 循环的单次传递。为了将其范围扩大到循环的多次传递,需要在循环外部声明它们。

关于java - 为什么当我尝试更改此 case 语句中的变量时,它不会更改该循环之外的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043261/

相关文章:

java - Jsoup 可以根据其类清理元素吗?

java - while true try catch 嵌套

java - 在java中访问while循环内的值?

javascript - 使用 ngRepeat 时限制显示结果的数量

java - Swing:如何将自定义光标设置为透明?

java - 不能用Hibernate调用PostgreSQL的11存储过程

c# - 混合模式调试

javascript - 为什么我保存的 D3 选择在某些情况下没有效果?

java - 创建 ArrayList 时应为 '(' 或 '['

C# 有意循环。 (CPU 使用率低)