2016-01-08 42 views
-2

我正在創建一個程序,其中涉及用戶必須以某種格式輸入關於遊戲的數據,他們將輸入幾條信息,然後我必須向他們顯示這些信息,包括他們輸入的遊戲的總分和總時間。我目前正在使用數組來存儲這些信息,但我無法計算出他們給我的所有數據。這是我迄今爲止。如何使用數組添加用戶輸入的數據並顯示它?

//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(); 
+0

請顯示您的完整代碼 –

+0

*完整代碼*我們的意思是[在此](http://sscce.org/)所述的內容。即編譯和展示您的問題的一段代碼。例如,這應該清楚「輸入」實際上是什麼。 – morido

+0

這不是完整的代碼。 'score'是什麼類型?我沒有看到你總結的那段代碼在哪裏出場? –

回答

0

嘗試刪除第一player = scanner.nextLine();
而替換此while循環:

while (player.isEmpty()) 

有了:

while (scanner.hasNextLine()){ 
    ... 
0

最好是與存儲的三個參數模型的類來做到這一點。但是,如果您想通過使用分隔符(:)拆分輸入行來執行臨時數組,則此示例是一個工作示例;

import java.util.LinkedList; 
import java.util.List; 
import java.util.Scanner; 

public class TestAnswer2 { 

    public static final String SEPARATOR = ":"; 

    public static void main(String[] args) { 

     List<String[]> dataList = new LinkedList<String[]>(); 
     String[] tempStringArray = null; 
     String playerName = null; 

     Scanner scanner = new Scanner(System.in); 
     String input = ""; 

     // If the player does not enter a name they are them prompted to do so 
     // again until they have. 
     while (playerName == null || playerName.isEmpty()) { 
      System.out.print("Enter your name: "); 
      playerName = scanner.nextLine(); 
     } 


     boolean shouldContinue = true; 
     while (shouldContinue) { 
      System.out.println("Enter game information seperated with colons. Example - Score:Mins"); 
      input = scanner.nextLine(); 
      tempStringArray = input.split(SEPARATOR); 

      if(tempStringArray.length == 1) {    
       if(tempStringArray[0].equalsIgnoreCase("quit")) { 
        shouldContinue = false; 
       } 
      } else if (tempStringArray.length == 2) { 
       System.out.println("Entered>> Score: " + tempStringArray[0] + ", Time: " + tempStringArray[1]); 
       dataList.add(tempStringArray); 
      } else { 
       System.out.println("Please Re-Enter game information seperated with colons. Example - Score:Mins"); 
      } 
     } 

     System.out.println("Player: " + playerName); 

     for(int i = 0; i < dataList.size(); i++) { 
      System.out.println("- - - - - - - - - - - - -"); 
      System.out.println("Game Index: " + (i+1) + ", " + "Score: " + dataList.get(i)[0] + ", " + "Time played: " + dataList.get(i)[1]); 

     } 

     scanner.close(); 
    } 

} 

並輸出;

Enter your name: Levent 
Enter game information seperated with colons. Example - Score:Mins 
56:4 
Entered>> Score: 56, Time: 4 
Enter game information seperated with colons. Example - Score:Mins 
112:7 
Entered>> Score: 112, Time: 7 
Enter game information seperated with colons. Example - Score:Mins 
43:5 
Entered>> Score: 43, Time: 5 
Enter game information seperated with colons. Example - Score:Mins 
366:12 
Entered>> Score: 366, Time: 12 
Enter game information seperated with colons. Example - Score:Mins 
quit 
Player: Levent 
- - - - - - - - - - - - - 
Game Index: 1, Score: 56, Time played: 4 
- - - - - - - - - - - - - 
Game Index: 2, Score: 112, Time played: 7 
- - - - - - - - - - - - - 
Game Index: 3, Score: 43, Time played: 5 
- - - - - - - - - - - - - 
Game Index: 4, Score: 366, Time played: 12 

您應該注意到遊戲索引自動遞增,並且鏈接列表僅存儲用於存儲分數和時間變量的數組。

希望它有幫助。

相關問題