如何使用数组添加用户输入的数据并显示它?

时间:2016-01-08 14:39:24

标签: java arrays split java.util.scanner string-split

我正在创建一个程序,该程序涉及用户必须以特定格式输入有关游戏的数据,他们将输入几条信息,然后我必须向他们显示这些信息,包括总得分和他们玩游戏的总时间输入。我目前正在使用数组来存储信息,但我无法弄清楚如何添加他们给我的所有数据。这就是我到目前为止所做的。

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

2 个答案:

答案 0 :(得分:0)

尝试删除第一个player = scanner.nextLine(); 并替换此while循环:

 while (player.isEmpty())

使用:

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

答案 1 :(得分:0)

最好使用存储三个参数的Model类来完成此操作。但是,如果您想通过使用分隔符(:)拆分输入行来使用临时数组,这是一个有效的示例;

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

您应该注意到游戏索引会自动增加,而链接列表只会存储用于存储分数和时间变量的数组。

希望它有所帮助。