Java数组-(并行数组)
我正在努力使用Java数组-(并行数组),java,arrays,Java,Arrays,我正在努力使用数组(java) 因为这是我一生中第一次学习java,所以我不知道如何开始。我刚刚学习了如何声明数组等等。然而,这对我来说太复杂了。如果我看到这个答案,我想我可以得到指导。有人能帮我吗 程序读取java.util.Scanner。你的代码可以工作。你可以对分数做你想做的事情,只需从文件中读取它们,并将它们转换为适当的数据类型(整数),以计算平均分数等 变量是字符串,因此必须将它们转换为数字才能进行计算 您可以使用ArrayList作为团队成员的实例变量,或者作为团队原始类型的集合,
数组
(java)
因为这是我一生中第一次学习java,所以我不知道如何开始。我刚刚学习了如何声明数组等等。然而,这对我来说太复杂了。如果我看到这个答案,我想我可以得到指导。有人能帮我吗
程序读取
java.util.Scanner
。你的代码可以工作。你可以对分数做你想做的事情,只需从文件中读取它们,并将它们转换为适当的数据类型(整数),以计算平均分数等
变量是字符串,因此必须将它们转换为数字才能进行计算
您可以使用ArrayList
作为团队成员的实例变量,或者作为团队原始类型的集合,但我认为最好的方法是为团队、团队成员和得分制作课程,并在保龄球课中实例化。你可以在任何地方找到这些信息
import java.util.Scanner;
//...
Team usa = new Team();
Team mexico = new Team();
TeamMember person = new TeamMember(usa); //Harry plays for the US
...
Scanner in = new Scanner(System.in);
int num = in.nextInt();
如果您知道每三个输入是一个分数,那么您可以检查模3(
%3
),以了解哪个迭代可以被3整除 我已经完成了你40%的请求,我想这足够让你继续下去了。你应该能自己完成它
如果你有进一步的问题,请留下评论。
(有一些隐藏的bug需要您处理,您应该首先了解范围,这只是为了学习。)
import java.io.*;
导入java.util.*;
//班级宣言
公共保龄球2{
//在下面声明数组
弦乐队成员;
整数分,分数,分数B;
int[]teamw=新int[10][3];
int[]teamb=新int[10][3];
//主程序声明
公共静态void main(字符串[]args)引发FileNotFoundException{
//1.连接到输入文件
Scanner fin=新的扫描仪(新的文件阅读器(“bowling.txt”);
//2)将阵列累加器初始化为零
int i,j,Scoreb,Scorew=0;
//3)显示描述性消息
System.out.println(
“此程序从bowling.txt文件中读取行以确定\n”
+保龄球比赛的获胜者。获胜的队伍、成员和分数\n
+“显示在监视器上。\n”);
//4)测试Scanner.eof()条件
while(fin.hasNext()){
//5)尝试从文件中输入下一行
成员=fin.next();
团队=fin.next();
分数=fin.nextInt();
//6)测试团队颜色为蓝色
if(Team.toString()=“蓝色”){
//7)然后存储蓝色成员并打分
teamb[i][0]=成员;
teamb[i][1]=团队;
teamb[i][2]=得分;
//8)增加蓝色数组累加器
sumArray(“蓝色”);
i++;
}
//9)其他存储白色成员和分数
否则{
teamw[j][0]=成员;
teamw[j][1]=团队;
teamw[j][2]=得分;
//10)增加白色阵列累加器
sumArray(“白色”);
j++;
}
}
//11)如果蓝队得分较大
//12)然后显示蓝色团队为赢家
//13)否则显示白色团队为赢家
//14断开与输入文件的连接
fin.close();
}
//实现下面的方法'sumArray()
/*1.将累加器初始化为0
2.循环初始化数组索引
3.通过索引数组元素增加累加器
4.回油蓄能器
*/
公共双色数组(字符串颜色){
如果(颜色=“蓝色”){
对于(int k=0;k
家庭作业?:)我建议使用一个类来表示每个团队成员,而不是3个单独的变量。然后,可以在从文件加载每个团队成员对象时创建它们,并将此对象添加到数组中。然后,您的函数需要循环这个数组(或使用lambda)来计算分数(使用if语句来确定将分数添加到哪个团队);然后将获胜团队的颜色存储为字符串值,再次循环以显示与该颜色匹配的所有团队成员(再次使用if语句)。这有帮助吗?您将变量名大写,这在Java中非常不常见;这会使阅读变得更难,你的老师会扣分。将“团队、成员和得分”改为小写。(团队、成员和分数)另外,在代码示例中。。。您尚未使用任何数组。此外,如果需要使用数组而不是ArrayList,则需要连续重新创建数组,因为您不知道正在加载多少团队成员。这并不难,看看……为什么分数会是一门单独的课?它不是TeamMember(个人分数)和Team(如果要实现该类,则为累积分数)中的一个字段吗。@CoolBots您可能希望将分数与自定义比较器进行比较?它们不是整数吗?我个人认为为本质上相当于一个整数的东西创建一个分数类和自定义比较器有点过火。。。你有这样一个自定义比较器的用例吗?@CoolBots是的,但是如果你只知道一个分数,你可能想从分数中了解更多关于游戏的信息。
import java.io.*;
import java.util.*;
// declaration of the class
public class Bowling2 {
// declare arrays below
String Team, Member;
int Score, Scorew, Scoreb;
int[][] teamw = new int[10][3];
int[][] teamb = new int[10][3];
// declaration of main program
public static void main(String[] args) throws FileNotFoundException {
// 1. connect to input file
Scanner fin = new Scanner(new FileReader("bowling.txt"));
// 2) initialize array accumulators to zero
int i, j, Scoreb, Scorew = 0 ;
// 3) display a descriptive message
System.out.println(
"This program reads the lines from the file bowling.txt to determine\n"
+ "the winner of a bowling match. The winning team, members and scores\n"
+ "are displayed on the monitor.\n");
// 4) test Scanner.eof() condition
while (fin.hasNext()) {
// 5) attempt to input next line from file
Member = fin.next();
Team = fin.next();
Score = fin.nextInt();
// 6) test team color is blue
if (Team.toString() == "blue" ){
// 7) then store blue member and score
teamb[i][0] = Member;
teamb[i][1] = Team;
teamb[i][2] = Score;
// 8) increase blue array accumulator
sumArray("blue");
i++;
}
// 9) else store white member and score
else {
teamw[j][0] = Member;
teamw[j][1] = Team;
teamw[j][2] = Score;
// 10) increase white array accumulator
sumArray("white");
j++;
}
}
// 11) if blue team score is larger
// 12) then display blue team as winner
// 13) else display white team as winner
// 14 disconnect from the input file
fin.close();
}
// implement method `sumArray()` below
/* 1. initialize accumulator to 0
2. loop over initialized array indices
3. increase accumulator by indexed array element
4. return accumulator
*/
public double sumArray(string color) {
if (color == "blue") {
for (int k = 0;k < teamb.length(); k++) {
//do the calculation here, and return it
}
}else {
for (int h = 0;h < teamw.length(); h++) {
//do the calculation here, and return it
}
}
}
// implement method `printArray()` below
/* 1. display the team name as the winner
2. loop over initialized array indices
3. display member and score for that array index
*/
}