我试图计算我的数组中的单词数量

时间:2016-01-11 02:45:41

标签: java arrays

我知道有很多类似的问题,但我无法理解,所以我发布这个,希望有人可以给我一些指导而不是作为副本投票。

所以基本上......我试图计算我的数组中的所有单词并将其转换为整数以输出结果。所以我的游戏名称数组就是我试图转换的内容。我试图将存储在那里的游戏转换为整数,以便输出结果,如下所示....

PLayers name : ......
Total games: .....
Total score: .....
Total minutes played: ....

下面我将向您展示我已经拥有的代码......

import java.util.Scanner;

    public class overflow
    {
        public static void main(String[] args)
        {
            String name;

            String[] game = new String[100];
            int[] score = new int[100];
            int[] min = new int[100];

            int nextLine = 0;

            System.out.println("Can you enter your name, game, achievement score, and time played in the following format as shown below:");
            System.out.println("<NAME>");
            System.out.println("<GAME> : <SCORE> : <TIMEPLAYED>");
            System.out.println("Once you have finished your input please notify the program you have finished with the command 'quit':");

            Scanner keyboard = new Scanner(System.in); // initialise Scanner
            name = keyboard.nextLine(); // assign next line of input to name

            for (int index = 0; index < 100; index++)
            {
                String input = keyboard.nextLine(); // next line of input

                if (input.compareToIgnoreCase("quit") == 0)
                {
                    break;
                }
                nextLine = nextLine + 1;

                String[] variables = input.split(":"); // perform split
                if (variables.length != 3)
                {
                    System.out.println("Sorry...  You've incorrectly layed out your data.");
                    System.out.println("Please try again!");
                }
                game[index] = variables[0].trim(); // first token trimmed of whitespaces
                try
                {
                    score[index] = Integer.parseInt(variables[1].trim()); // Returns an integer/parse second token as int
                    min[index] = Integer.parseInt(variables[2].trim()); // Returns and integer/parse third token as int
                }
                catch (NumberFormatException e)
                {
                    System.out.println("Sorry... I was unable to convert that to a number.");
                }
            }

            System.out.println("Players name: " + name);
            System.out.println("-------------------------------");

            for (int index = 0; index < nextLine; index++)
            {

                System.out.println("Game: " + game[index] + " | " + "Total Score: " + score[index] + " | " + "Minutes played: " + min[index]);

            }

            System.out.println("");
            System.out.println("");
            System.out.println("Players name: " + name);
            System.out.println("-------------------------------");
            System.out.println("Total games: " + game.length);
            System.out.println("Overall score: " + score.length);
            System.out.println("Total minutes played: " + min.length);
        }
    }

下面我将显示我得到的输出正确的位,然后在下面我会告诉你输出不正确,我需要帮助。

正确:这是我需要的第一个输出。

Players name: Matty Cutts
-------------------------------
Game: Brink | Total Score: 213 | Minutes played: 323
Game: Fifa | Total Score: 12 | Minutes played: 21
Game: Call of Duty | Total Score: 31 | Minutes played: 13

不正确:这是我需要将上述游戏加在一起制作一个整数的地方,因为有3个游戏,因为有3个游戏,之后我需要添加所有分数,然后所有分钟都被添加到添加到然后以下列格式输入..

Players name: Matty Cutts
-------------------------------
Total games: 100
Overall score: 100
Total minutes played: 100

任何建议都将不胜感激。谢谢你的阅读。新年快乐。

1 个答案:

答案 0 :(得分:0)

你只需要三个变量,一个用于游戏总数,得分和玩的时间。

然后,当您在阵列上循环打印出信息时,您会计算结果,例如......

int totalGames = 0; // could be nextLine
int totalScore = 0;
int totalPlayed = 0;
for (int index = 0; index < nextLine; index++) {

    System.out.println("Game: " + game[index] + " | " + "Total Score: " + score[index] + " | " + "Minutes played: " + min[index]);
    totalGames++;
    totalScore += score[index];
    totalPlayed += min[index];

}

System.out.println("");
System.out.println("");
System.out.println("Players name: " + name);
System.out.println("-------------------------------");
System.out.println("Total games: " + totalGames);
System.out.println("Overall score: " + totalScore);
System.out.println("Total minutes played: " + totalPlayed);

<强>更新

  

如何在初始输入上检查这些?游戏条目中的数字值可能不代表有效的整数.....并且输入可能有空白行,即按下了返回但是“退出”尚未输入

public class overflow
{
    public static void main(String[] args)
    {
        String name;

        String[] game = new String[100];
        int[] score = new int[100];
        int[] min = new int[100];

        int nextLine = 0;

        System.out.println("Can you enter your name, game, achievement score, and time played in the following format as shown below:");
        System.out.println("<NAME>");
        System.out.println("<GAME> : <SCORE> : <TIMEPLAYED>");
        System.out.println("Once you have finished your input please notify the program you have finished with the command 'quit':");

        Scanner keyboard = new Scanner(System.in); // initialise Scanner
        name = keyboard.nextLine(); // assign next line of input to name

        int totalGames = 0; // could be nextLine
        int totalScore = 0;
        int totalPlayed = 0;
        int highestScore = 0;

        for (int index = 0; index < 100; index++)
        {
            String input = keyboard.nextLine(); // next line of input

            if (input.compareToIgnoreCase("quit") == 0)
            {
                break;
            }
            nextLine = nextLine + 1;

            String[] variables = input.split(":"); // perform split
            if (variables.length != 3)
            {
                System.out.println("Sorry...  You've incorrectly layed out your data.");
                System.out.println("Please try again!");
            }
            game[index] = variables[0].trim(); // first token trimmed of whitespaces
            try
            {
                score[index] = Integer.parseInt(variables[1].trim()); // Returns an integer/parse second token as int
                min[index] = Integer.parseInt(variables[2].trim()); // Returns and integer/parse third token as int

                totalGames++;
                totalScore += score[index];
                totalPlayed += min[index];
                highestScore = Math.max(highestScore, score[index]);
            }
            catch (NumberFormatException e)
            {
                System.out.println("Sorry... I was unable to convert that to a number.");
            }
        }

        System.out.println("Players name: " + name);
        System.out.println("-------------------------------");

        for (int index = 0; index < nextLine; index++) {

            System.out.println("Game: " + game[index] + " | " + "Total Score: " + score[index] + " | " + "Minutes played: " + min[index]);

        }

        System.out.println("");
        System.out.println("");
        System.out.println("Players name: " + name);
        System.out.println("-------------------------------");
        System.out.println("Total games: " + totalGames);
        System.out.println("Overall score: " + totalScore);
        System.out.println("Total minutes played: " + totalPlayed);
    }
}