【问题标题】:Why is it that when i try to change a variable in this case statement it doesn't change the variable outside that loop为什么当我尝试在这个 case 语句中更改变量时,它不会更改该循环之外的变量
【发布时间】:2015-07-14 14:43:59
【问题描述】:

为什么在我开始时会发生这种情况(?,我刚刚声明了变量的名称,我不确定那叫什么太累而无法知道)案例之外的变量,为什么不只是改变多变的。这不像我正在制作它的一个实例。为什么我必须做一个实例?这似乎是我应该能够做的一件简单的事情。我如何在不进行任何静态变量的情况下解决这个问题。

我在下面有一些我正在处理的代码,我想我把它全部留在里面,但是看看第三种情况,当我尝试运行它时,我在第二种情况下更改的内容不会影响变量,例如名称[] 和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循环中,你不断地初始化你的名字变量。在循环外声明并初始化“名称”。
  • 单步调试器下的代码。当您认为变量应该更改时,请查看它们,并确保实际执行了部分代码。还要确保您不会无意中重新初始化要保留的数据。调试器是你的朋友 :)

标签: java while-loop scope initialization


【解决方案1】:

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

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

根据变量范围解释上述内容:尽管您已在 case 语句之外声明变量,但它们仍然while 循环中,这限制了它们的范围 到 while 循环的单遍。要将它们的范围扩大到循环的多次传递,需要在循环之外声明它们。

【讨论】:

    【解决方案2】:
            String[] names;
            String fileOutput = "names.txt";
            int amountOfNames = 0;
            int amountOfNames2 = 0;
            int x = 0;
            names = new String[1];
            names[0] = "";
    

    在 do while 循环之外声明所有这些变量。每当循环执行所有这些变量初始化。如果要初始化任何变量,请将这些变量保留在循环内,否则将变量移到循环外以避免每次都重新初始化。

    【讨论】:

      【解决方案3】:

      在每次循环迭代后重置值,并且不会在 case 语句之外打印值。睡一觉,然后重新阅读代码。

      【讨论】:

        【解决方案4】:

        您每次循环时都在重新初始化变量,而不是修改它们。将它们移到循环之外,如下所示:

        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";
        
                String[] names;
                String fileOutput = "names.txt";
                int amountOfNames = 0;
                int amountOfNames2 = 0;
                int x = 0;
                names = new String[1];
                names[0] = "";
        
                while(doProgram.equals("yes"))
                {
                    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");
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2011-09-04
          • 2018-07-06
          • 1970-01-01
          • 1970-01-01
          • 2022-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-04
          相关资源
          最近更新 更多