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
我在while循环之外的每个对所有者对象的调用中遇到错误,说"所有者#无法解析为变量"。如果我在循环之前声明对象,我会在循环中创建的对象上出现重复的对象错误。我不太确定如何使这项工作。
答案 0 :(得分:0)
您已在while循环内同时声明并初始化变量owner1,owner2,owner3。您可以做的是,首先在(外部)循环之前声明并初始化变量,并在while循环中创建所有者对象,如下所示:
csv.reader
同样对其他两个所有者变量执行此操作。阅读变量范围以便更好地理解。
http://www.java-made-easy.com/variable-scope.html
如果要创建多个所有者对象,则可以在while循环上方创建一个列表对象,并将所有者对象添加到该列表中,并在打印值时从列表中检索添加的对象。
答案 1 :(得分:0)
如果要在while和while中访问这些变量,则需要在外部声明它们。在您的情况下,这些变量只在while内部知道,因为它们已经被声明。尝试阅读可变范围。
尝试这样的事情:
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);
}