我正在創建一個程序,其中涉及用戶必須以某種格式輸入關於遊戲的數據,他們將輸入幾條信息,然後我必須向他們顯示這些信息,包括他們輸入的遊戲的總分和總時間。我目前正在使用數組來存儲這些信息,但我無法計算出他們給我的所有數據。這是我迄今爲止。如何使用數組添加用戶輸入的數據並顯示它?
//setting up my array.
myArray = input.split(":");
score = Integer.parseInt(myArray[1]);
timePlayed = Integer.parseInt(myArray[2]);
這是我設置陣列分配器。
do {
numGames++;
System.out.println("Enter game information seperated with colons. Example - Game:Score:Mins");
input = scanner.nextLine();
這就是我要求用戶以我所要求的格式輸入數據的地方。這將重複進行,直到他們進入「退出」或達到20
}while (!input.equalsIgnoreCase("quit") && numGames <20);
System.out.println("Player: " + player);
System.out.println("- - - - - - - - - - - - -");
System.out.println("Games played: " + numGames + ", " + "Score: " + score + ", " + "Time played: " + timePlayed);
遊戲的極限,然後我要顯示這樣的信息,然而,當我運行我的程序並不加起來組合遊戲時間,遊戲數量的作品,但分數和時間播放不。
對不起,如果這是格式不正確,因爲它是我的第一篇文章!如果你們需要我的程序中的更多信息或代碼來幫助我讓我知道,我會盡力的!謝謝一堆! :)
要求全碼:
String input;
String player;
int score;
int timePlayed;
float scores;
float time;
Scanner scanner = new Scanner (System.in);
int numGames = 0;
System.out.print("Enter your name: ");
player = scanner.nextLine();
//If the player does not enter a name they are them prompted to do so again until they have.
while (player.isEmpty())
{
System.out.print("Enter your name: ");
player = scanner.nextLine();
}
String[] myArray = new String [2];
//This section is prompting the player to input game data in a format requested.
do {
numGames++;
System.out.println("Enter game information seperated with colons. Example - Game:Score:Mins");
input = scanner.nextLine();
//If the player does not enter any information then this piece of code will run and ask them to do so.
while (input.isEmpty())
{
System.out.println("Enter game information seperated with colons. Example - Game:Score:Mins");
input = scanner.nextLine();
}
//setting up my array.
myArray = input.split(":");
score = Integer.parseInt(myArray[1]);
timePlayed = Integer.parseInt(myArray[2]);
//This displays to the player what they have just entered.
System.out.println("Player: " + player);
System.out.println("- - - - - - - - - - - - -");
System.out.println("Games played: " + numGames + ", " + "Score: " + score + ", " + "Time played: " + timePlayed);
input = scanner.nextLine();
try{
scores = Float.parseFloat(myArray[1]);
time = Float.parseFloat(myArray[2]);
}
catch (NumberFormatException e) {
System.out.println("Error: invalid input, not a number" + e.getMessage());
}
input = scanner.nextLine();
score.add(myArray[0]);
}while (!input.equalsIgnoreCase("quit") && numGames <2);
System.out.println("Player: " + player);
System.out.println("- - - - - - - - - - - - -");
System.out.println("Games played: " + numGames + ", " + "Score: " + score + ", " + "Time played: " + myArray[2]);
System.out.println("Exit");
scanner.close();
請顯示您的完整代碼 –
*完整代碼*我們的意思是[在此](http://sscce.org/)所述的內容。即編譯和展示您的問題的一段代碼。例如,這應該清楚「輸入」實際上是什麼。 – morido
這不是完整的代碼。 'score'是什麼類型?我沒有看到你總結的那段代碼在哪裏出場? –