简体   繁体   English

Java无法识别循环外的循环中创建的对象

[英]Java not recognizing objects that are created in a loop, outside of the loop

    String nameFromFile;
    String colorFromFile;
    int capacityFromFile;
    int currentCountFromFile;


    while(inputFile.hasNextLine()){
        nameFromFile = inputFile.next();
        colorFromFile = inputFile.next();
        capacityFromFile = inputFile.nextInt();
        currentCountFromFile = inputFile.nextInt();

        MarbleSackOwner owner1 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);


        nameFromFile = inputFile.next();
        colorFromFile = inputFile.next();
        capacityFromFile = inputFile.nextInt();
        currentCountFromFile = inputFile.nextInt();

        MarbleSackOwner owner2 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);

        nameFromFile = inputFile.next();
        colorFromFile = inputFile.next();
        capacityFromFile = inputFile.nextInt();
        currentCountFromFile = inputFile.nextInt();

        MarbleSackOwner owner3 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);

        nameFromFile = inputFile.next();
        colorFromFile = inputFile.next();
        capacityFromFile = inputFile.nextInt();
        currentCountFromFile = inputFile.nextInt();

        MarbleSackOwner owner4 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);

    }
    String output = "Owners after creaton based on file read \n";
    output += "Owner1: " + owner1 + "\n";
    output += "Owner2: " + owner2 + "\n";
    output += "Owner3: " + owner3 + "\n";
    output += "Owner4: " + owner4 + "\n";

    output += "owner1's name is " + owner1.getName() + "\n";
    output += "like I said, owner1 has " + owner1.howManyMarbles() + 
                " marbles in his sack.\n";
    output += "So lets take one away from him.\n";
    owner1.removeMarbles(1);
    output += "so now we see that he has " + owner1.howManyMarbles() +
                " in his sack.\n";
    output += "so lets give the darn thing back to him now.\n";
    owner1.addMarbles(1);
    output += "so now we see that he has " + owner1.howManyMarbles() +
                " in his sack.\n";

    //mess with owner4
    output += "so, maybe " + owner4.getName() +
                " has lost a marble or two, so lets give him back one.\n";
    owner4.addMarbles(1);
    output += "So now we see that he has " + owner4.howManyMarbles() + 
                " marbles in his sack.\n\n";

    //test class method
    output += "the owner with the most marbles is " +
                bigOwner(owner1, owner2, owner3, owner4);

    //results
    JOptionPane.showMessageDialog(null, output, TITLE_BAR, JOptionPane.INFORMATION_MESSAGE);

}//main

I am getting errors on every call to a owner object outside of the while loop saying "owner# cannot be resolved to a variable". 我在while循环外每次调用所有者对象时都遇到错误,说“ owner#无法解析为变量”。 if I declare the objects before the loop, I get a duplicate object error on the objects created within the loop. 如果在循环之前声明对象,则会在循环内创建的对象上收到重复的对象错误。 I'm not quite sure how to make this work. 我不太确定如何进行这项工作。

You have declared and initialized your variables owner1, owner2, owner3 at the same time inside while loop. 您已在while循环中同时声明并初始化了变量owner1,owner2,owner3。 What you can do is, First declare and initialize the variable before (outside) loop and create owner objects within while loop like this: 您可以做的是,首先在(外部)循环之前声明并初始化变量,然后在while循环内创建所有者对象,如下所示:

MarbleSackOwner owner1 = null;
...
while(..){
...
owner1 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);
...
}

Similarly do this action for other two owner variables. 同样,对其他两个所有者变量执行此操作。 Read through scope of variables for better understanding. 通读变量的范围以更好地理解。

http://www.java-made-easy.com/variable-scope.html http://www.java-made-easy.com/variable-scope.html

If you are going to create multiple owner objects then you can create a list object above your while loop and add the owner objects to that list and retriev the added objects from your list while printing the values. 如果要创建多个所有者对象,则可以在while循环上方创建一个列表对象,然后将所有者对象添加到该列表中,并在打印值时从列表中检索添加的对象。

If you want access to these variables in and out of the while, you need to declare them outside. 如果要不时访问这些变量,则需要在外部声明它们。 In your case, these variables are know only inside the while cause that's where they have been declared. 在您的情况下,这些变量仅在while原因内才知道,因为那是声明它们的地方。 Try reading up on variable scopes. 尝试阅读可变范围。

Try something like this: 尝试这样的事情:

MarbleSackOwner owner1 = new MarbleSackOwner(string.Empty, string.Empty, 0, 0),
owner2 = new MarbleSackOwner(string.Empty, string.Empty, 0, 0),
owner3 = new MarbleSackOwner(string.Empty, string.Empty, 0, 0),
owner4 = new MarbleSackOwner(string.Empty, string.Empty, 0, 0);    

while(inputFile.hasNextLine()){
        nameFromFile = inputFile.next();
        colorFromFile = inputFile.next();
        capacityFromFile = inputFile.nextInt();
        currentCountFromFile = inputFile.nextInt();

        owner1 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);


        nameFromFile = inputFile.next();
        colorFromFile = inputFile.next();
        capacityFromFile = inputFile.nextInt();
        currentCountFromFile = inputFile.nextInt();

        owner2 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);

        nameFromFile = inputFile.next();
        colorFromFile = inputFile.next();
        capacityFromFile = inputFile.nextInt();
        currentCountFromFile = inputFile.nextInt();

        owner3 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);

        nameFromFile = inputFile.next();
        colorFromFile = inputFile.next();
        capacityFromFile = inputFile.nextInt();
        currentCountFromFile = inputFile.nextInt();

        owner4 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM