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

时间:2015-05-05 02:24:29

标签: java while-loop scope initialization

当我开始(?,我只是声明变量的名称,我不确定那些被称为过于疲倦而知道)变量的情况时,为什么会发生这种情况呢?为什么不是只是改变了变量。这不像我正在制作它的实例。我为什么要制作它的实例?这似乎是一件我应该做的简单事情。如何在不进行任何变量静态的情况下解决这个问题。

我有一些我正在下面工作的代码,我认为不好只是把它全部放进去但是看看第3种情况,当我尝试运行它时我在第2种情况下改变的东西不会影响变量如名称[]和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");
        }
    }
}

你非常喜欢阅读我希望能帮助你与他们相处再见。

4 个答案:

答案 0 :(得分:1)

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

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

根据变量范围解释上述内容:虽然您已在case语句之外声明了变量,但它们仍然 while循环中,并且这些限制他们的范围到while循环的单个传递。要将它们的范围增加到循环的多次传递,需要在循环外声明它们。

答案 1 :(得分:0)

        String[] names;
        String fileOutput = "names.txt";
        int amountOfNames = 0;
        int amountOfNames2 = 0;
        int x = 0;
        names = new String[1];
        names[0] = "";

在while循环中声明所有这些变量。每当循环执行所有这些变量初始化。如果您想要初始化任何变量,那么将这些变量保留在循环中,否则将变量移出循环以避免每次都重新初始化。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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");
        }
    }
}
相关问题