我与Arrays
(JAVA)奋力Java数组 - (平行阵列)
由于这是第一次,了解我生活的java,我不知道如何启动它。我刚刚学会了如何声明数组等。但是,这对我来说太复杂了。我想我可以得到指导方针,如果我看到这个答案。有人可以帮我吗?
的PROG
我与Arrays
(JAVA)奋力Java数组 - (平行阵列)
由于这是第一次,了解我生活的java,我不知道如何启动它。我刚刚学会了如何声明数组等。但是,这对我来说太复杂了。我想我可以得到指导方针,如果我看到这个答案。有人可以帮我吗?
的PROG
阅读java.util.Scanner
(API)。你的代码有效。你可以用分数来做你想要的,只需从文件中读取它们并将它们转换为合适的数据类型(整数)以计算平均得分等。
变量是字符串,因此您必须将它们转换为数字做你的计算。
您可以使用arraylistArrayList<TeamMember>
作为TeamMembers为您的团队的一个实例变量或基本类型为球队的集合,但我认为最好的是,如果你让班队TeamMember和分数您在实例化你的保龄球课程。您可以在任何地方找到此信息。
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.*;
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
*/
}
家庭作业? :)我建议使用一个类来代表每个团队成员而不是3个单独的变量。然后,您可以在从文件加载它们时创建每个团队成员对象,并将此对象添加到数组中。然后你的函数需要遍历这个数组(或使用lambda)来计算分数(使用if语句来确定哪个团队添加分数);然后将获胜团队的颜色存储为字符串值,再次循环显示与该颜色匹配的所有团队成员(再次使用if语句)。这有帮助吗? – CoolBots
你有你的变量名字大写,这在Java中是非常不常见的;这会让阅读变得更加困难,而你的教练将会取消积分。使“团队,成员和分数”小写。 (团队,成员和分数) –
另外,在你的代码示例中,你还没有使用任何数组。 –