我知道有很多与此类似的问题,但我就是无法理解,所以我发布此内容是希望有人能给我一些指导,而不是将其视为重复。
所以基本上...我试图计算数组中的所有单词并将其转换为整数以输出结果。所以我的游戏名称数组就是我想要转换的。我试图将存储在其中的游戏转换为整数,以便输出结果如下......
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
任何建议将不胜感激。感谢您的阅读。新年快乐。
最佳答案
您只需要另外三个变量,其中一个表示比赛总数、得分和比赛时间。
然后,当您循环遍历数组并打印出信息时,您会计算结果,例如......
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);
已更新
How can i check for these on the initial inputs? Numeric values in a game entry may not represent valid integer numbers..... and The input may have blank lines, i.e. return is pressed but “quit” hasn’t been typed yet
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);
}
}
关于java - 我正在尝试计算数组中的单词数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713440/