Java 我试图有多行输入,并将字符串、整数存储到三个独立的变量中
因此,我尝试存储多行代码,并将每个输入添加到特定数组下。所以基本上,一个人输入游戏的名字,这个名字被存储到一个叫做“游戏”的数组中,然后他们在同一行上输入他们已经获得的分数,这应该被存储到aray的“分数”中,并且在游戏的分钟数中也是如此。你明白我的意思 输入应如下所示Java 我试图有多行输入,并将字符串、整数存储到三个独立的变量中,java,arrays,Java,Arrays,因此,我尝试存储多行代码,并将每个输入添加到特定数组下。所以基本上,一个人输入游戏的名字,这个名字被存储到一个叫做“游戏”的数组中,然后他们在同一行上输入他们已经获得的分数,这应该被存储到aray的“分数”中,并且在游戏的分钟数中也是如此。你明白我的意思 输入应如下所示 <name of player> <game name> : <score> : <min played> 这就是它实际上看起来的样子 PLayers Name: Matty
<name of player>
<game name> : <score> : <min played>
这就是它实际上看起来的样子
PLayers Name: Matty
Number of games: GTA IV
Total Score: 12
Total minutes played: 124
正如你所看到的,它基本上只拆分第一行,并将其放入这些标题中,当我希望它将每个部分加起来,并将它们放入我想要的实际输出中时。它只拆分第一个游戏数据,不将其与下面的其他行相加
这是我的伪代码
get name of player and store in a variable
loop for 100
get input
if it is "quit" then break
split up input
if I don't get 3 things then it is invalid
else if I do get 3 things
store the first one in an array for game names
store the second one in an array for scores
store the third one in an array for times
if I can’t convert scores &/or times - it is invalid
end loop
output player’s name
output a line of “------------------------”
loop for how many valid games we have
output a line containing gamename, score, minutes played
end loop
然后把所有的游戏加起来,变成一个整数作为最终输出,如上图所示,然后对所有的分数和分钟进行同样的处理,并按上图所示打印出来……我不知道我是否理解正确,但你有什么类似的事情吗
import java.util.Scanner;
public class Report
{
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)
{
// handle invalid input here
}
game[index] = variables[0].trim(); // first token trimmed of whitespaces
try
{
score[index] = Integer.parseInt(variables[1].trim()); // parse second token as int
min[index] = Integer.parseInt(variables[2].trim()); // parse third token as int
}
catch (NumberFormatException e)
{
// handle invalid input here
}
}
for (int index = 0; index < nextLine; index++)
{
System.out.println("Players name: " + name);
System.out.println("-------------------------------");
System.out.println("Total games: " + game[index]);
System.out.println("Overall score: " + score[index]);
System.out.println("Total minutes played: " + min[index]);
System.out.println(game[index]);
}
}
}
import java.util.Scanner;
公开课报告
{
公共静态void main(字符串[]args)
{
字符串名;
字符串[]游戏=新字符串[100];
整数[]分数=新整数[100];
int[]min=新int[100];
int nextLine=0;
System.out.println(“你能按如下格式输入你的名字、游戏、成就分数和玩的时间吗?”);
System.out.println(“”);
System.out.println(“::”);
System.out.println(“完成输入后,请使用命令'quit':'通知已完成的程序;”;
扫描仪键盘=新扫描仪(System.in);//初始化扫描仪
name=keyboard.nextLine();//将下一行输入指定给name
对于(int-index=0;index<100;index++)
{
字符串输入=键盘.nextLine();//下一行输入
if(input.compareTignoreCase(“quit”)==0)
{
打破
}
下一行=下一行+1;
String[]variables=input.split(:“”;//执行拆分
if(variables.length!=3)
{
//在此处处理无效输入
}
game[index]=变量[0].trim();//第一个标记修剪了空格
尝试
{
score[index]=Integer.parseInt(变量[1].trim());//将第二个标记解析为int
min[index]=Integer.parseInt(变量[2].trim());//将第三个标记解析为int
}
捕获(数字格式)
{
//在此处处理无效输入
}
}
对于(int index=0;index
也许你应该看看,或者我试过扫描仪,但我的讲师告诉我,我不应该使用扫描仪,因为你在:
上拆分字符串,你有基本的输入要求。你有什么问题?现在我们知道了典型的输出,你的实际输出是什么样子的?你想让用户输入许多玩家的名字和每个人的许多游戏吗?Bercik我爱你!这基本上就是我想要的第一点。然后我想让它把所有的数据加起来,这样它就有了每一个的总数量,游戏的数量,分数和游戏时间。要循环你的例如分数数组中的每个元素,将它解析为整数(因为它是字符串)和一些变量求和,例如scoresSumSo我如何计算输入的游戏名的数量,并将其转换为整数,然后输出
get name of player and store in a variable
loop for 100
get input
if it is "quit" then break
split up input
if I don't get 3 things then it is invalid
else if I do get 3 things
store the first one in an array for game names
store the second one in an array for scores
store the third one in an array for times
if I can’t convert scores &/or times - it is invalid
end loop
output player’s name
output a line of “------------------------”
loop for how many valid games we have
output a line containing gamename, score, minutes played
end loop
import java.util.Scanner;
public class Report
{
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)
{
// handle invalid input here
}
game[index] = variables[0].trim(); // first token trimmed of whitespaces
try
{
score[index] = Integer.parseInt(variables[1].trim()); // parse second token as int
min[index] = Integer.parseInt(variables[2].trim()); // parse third token as int
}
catch (NumberFormatException e)
{
// handle invalid input here
}
}
for (int index = 0; index < nextLine; index++)
{
System.out.println("Players name: " + name);
System.out.println("-------------------------------");
System.out.println("Total games: " + game[index]);
System.out.println("Overall score: " + score[index]);
System.out.println("Total minutes played: " + min[index]);
System.out.println(game[index]);
}
}
}