import java.io.*;
public class SaveAndLoad
{
private Scoreboard[] scoreboard = new Scoreboard[5];
//constructor
public SaveAndLoad(Scoreboard[] scoreboard)
{
this.scoreboard = scoreboard;
}
public static void save() throws FileNotFoundException
{
// 1. Create 5 GameScores
// 2. Put 5 GameScores into a gamescore[]
// 3. Create a Scoreboard using a gamescore[]
//Create 5 instances of the Game Class
GameScore player1 = new GameScore("Rebekah", 199);
GameScore player2 = new GameScore("Allen", 195);
GameScore player3 = new GameScore("Sami", 198);
GameScore player4 = new GameScore("Megan", 142);
GameScore player5 = new GameScore("Vince", 169);
GameScore[] gamescore = new GameScore[5];
gamescore[0] = player1;
gamescore[1] = player2;
gamescore[2] = player3;
gamescore[3] = player4;
gamescore[4] = player5;
//Create an instance of the Scoreboard Class using the GameScore array
Scoreboard scoreboard = new Scoreboard(GameScore[] gamescore);
最后一行給了我一個錯誤,我不知道為什么,因為這些是 Scoreboard 類的引數。
uj5u.com熱心網友回復:
簡答
改成:
Scoreboard scoreboard = new Scoreboard(gamescore);
更長的答案
考慮代碼:
new GameScore("Vince", 169);
你沒有做錯
new GameScore(String "Vince", int 169);
所以如你所知,沒有必要傳遞變數型別和值,實際上這樣做不僅沒有必要而且是錯誤的。
提示
為了使您的代碼更簡潔,您可以考慮如下撰寫代碼,消除不必要的變數名稱。
GameScore[] gamescore = new GameScore[5];
gamescore[0] = new GameScore("Rebekah", 199);
gamescore[1] = new GameScore("Allen", 195);
gamescore[2] = new GameScore("Sami", 198);
gamescore[3] = new GameScore("Megan", 142);
gamescore[4] = new GameScore("Vince", 169);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/353661.html
上一篇:高級前端進階(二)
