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循環說法之外的外部「所有者#不能被解析爲一個變量」。如果我在循環之前聲明對象,那麼在循環內創建的對象上會出現重複的對象錯誤。我不太清楚如何使這項工作。的Java不承認是在一個循環中創建的對象,循環
歡迎堆棧溢出。請正確地說*錯誤是什麼 - 理想情況下,儘量將代碼縮減爲*僅僅是一個展示問題的最小示例。有關更多信息,請參閱http://stackoverflow.com/help/mcve。 –
完成。我的道歉 –
你需要閱讀*變量範圍*。您的所有者變量超出了while循環的範圍。 – JonK