Java无法识别在循环中创建的对象(在循环之外)

Java无法识别在循环中创建的对象(在循环之外),java,oop,loops,Java,Oop,Loops,每次调用while循环之外的owner对象时,我都会遇到错误,说“owner#无法解析为变量”。如果在循环之前声明对象,则在循环中创建的对象上会出现重复对象错误。我不太清楚该怎么做 您已经在while循环中同时声明并初始化了变量owner1、owner2、owner3。您可以做的是,首先在(外部)循环之前声明并初始化变量,然后在while循环中创建所有者对象,如下所示: String nameFromFile; String colorFromFile; int capa

每次调用while循环之外的owner对象时,我都会遇到错误,说“owner#无法解析为变量”。如果在循环之前声明对象,则在循环中创建的对象上会出现重复对象错误。我不太清楚该怎么做

您已经在while循环中同时声明并初始化了变量owner1、owner2、owner3。您可以做的是,首先在(外部)循环之前声明并初始化变量,然后在while循环中创建所有者对象,如下所示:

    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
MarbleSackOwner owner1 = null;
...
while(..){
...
owner1 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);
...
}
类似地,对其他两个所有者变量执行此操作。通读变量的范围,以便更好地理解


如果要创建多个所有者对象,则可以在while循环上方创建一个列表对象,将所有者对象添加到该列表中,并在打印值时从列表中检索添加的对象。

如果要在while内外访问这些变量,则需要在外部声明它们。在您的例子中,这些变量只在while原因中知道,while原因就是它们被声明的地方。尝试读取变量范围

试着这样做:

    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
MarbleSackOwner owner1 = null;
...
while(..){
...
owner1 = new MarbleSackOwner(nameFromFile, colorFromFile,
                                    capacityFromFile, currentCountFromFile);
...
}

欢迎来到堆栈溢出。请准确地说出错误是什么——理想情况下,尽量将代码缩减为演示问题的最小示例。有关更多信息,请参阅。您需要了解变量作用域。您的所有者变量超出了while循环的范围。为循环之外的所有所有者变量添加了声明行,并将其初始化为null。现在似乎已经解决了这个问题。谢谢你,乔恩。