这里的说明是创建一个BowlingScores类和BowlingScoresViewer客户端类:在BowlingScores类中,创建一个名为printScores()的方法:
此课程应跟踪多个保龄球员的分数。分数是整数值 在0到300之间。
创建一个普通的一维数组来跟踪投球手的昵称。用一个 初始化列表,用6个礼炮昵称(6个元素)填充此数组。
创建一个二维数组,以跟踪每个投球手的多个分数
写一个循环来处理两个数组,如下所示:
Here is the link to the BowlerScoresViewer class
Here is the link to the BowlerScores class
现在这就是我在代码方面所需要的,我需要帮助找到平均数并确保我这样做。:
package javaconcepts;
// import javax.swing.JOptionPane;
//BowlingScores is used to practice using parallel arrays and 2-dimensional arrays
public class BowlingScores_MatthewBall
{
public void printScores()
{
System.out.println("BOWLING SCORES:");
String[] bowlers = {"L'Carpetron Dookmarriot", "Jackmerius Tacktheritrix", "Hingle McCringleberry", "Beezer Twelve Washingbeard", "X-Wing @Aliciousness", "Shakiraquan T.G.I.F. Carter"};
int[][] bowlerScores = {{110, 90, 160, 140, 110}, // row 0
{92, 96, 80, 112, 204}, // row 1
{200, 200, 200, 190, 210}, // row 2
{103, 90, 90, 150, 170}, // row 3
{118, 120, 116, 102, 200}, // row 4
{96, 96, 80, 112, 200}}; // row 5
/* for (int i = 0; i < bowlers[i].length; i++)
{
for (int x = 0; x < bowlerScores[x].length; i++)
{
System.out.println("L'Carpetron Dookmarriot bowled games of " + bowlerScores[x] + " which results in an average of " + ".");
System.out.println("Jackmerius Tacktheritrix bowled games of " + bowlerScores[1] + " which results in an average of " + ".");
System.out.println("Hingle McCringleberry bowled games of " + bowlerScores[2] + " which results in an average of " + ".");
System.out.println("Beezer Twelve Washingbeard bowled games of " + bowlerScores[3] + " which results in an average of " + ".");
System.out.println("X-Wing @Aliciousness bowled games of " + bowlerScores[4] + " which results in an average of " + ".");
System.out.println("Shakiraquan T.G.I.F. Carter bowled games of " + bowlerScores[5] + " which results in an average of " + ".");
}
}*/
for (int row = 0; row < bowlers.length; row++) //loops thru rows
{
System.out.println("");
System.out.print(bowlers[row] + " bowled games of " );
for (int col = 0; col < bowlerScores[0].length; col++) //loops thru columns
{
System.out.print( bowlerScores[row][col] + ", " );
}
System.out.print( "which results in an average of " );
for (int col = 0; col < bowlerScores[0].length; col++) //loops thru columns
{
System.out.print(bowlerScores[1][2]/bowlerScores[0].length);
}
}
}
答案 0 :(得分:0)
找到平均值: 将整数“total”设置为0。 然后创建for循环以将每个总计加起来。 在for循环之后,你用bowlerScores [#]。长度来划分总数。
此代码用于单行数组;显然你必须编辑多列。
for(int i = 0; i&lt; array.length; ++ i){
total = total + array[i];
}
int avg = total / array.length;
返回avg;
现在你可以做六次不同的时间,并存储6个不同的avg变量。
另外,理想情况下,使用for循环打印出每个人及其名字,你可以在保龄球阵列的每个位置使用字符串,因此你可以使用6个不同的打印线。这也很有用,因为你可以输入任何名字,而不是被那些名字所困扰,这实际上相当于无法将它们放入数组中。
如果您使用6个不同的avg变量进行上述更改而不是6 for循环,那么您将在每个循环中更新一个。
应该看起来像这样...
for(int x = 0; x&lt; bowlerScores [x] .length; i ++) {
System.out.println(bowlers[x] + " bowled games of " + bowlerScores[x] + " which results in an average of " + ".");
你可能不得不仔细检查一下,但是我还没有使用过多个拥有多个列的数组(我自己肯定是初学者)。
答案 1 :(得分:0)
不,我担心你错了,除非你的循环语法正确。然后,每次进行循环时,您都可以手动打印所有数组值。您可能已经知道了!我会给你那件作品,看看你是否能从那里得到一切:
System.out.println(bowlers[x]);
这将打印出所有投球手的名字。从那里,按照给出的说明,这是非常清楚的。您需要弄清楚如何在bowlerScores数组中初始化您的值;你只是在一个方面。您还需要将保龄球的数量放在保龄球阵列中,以便将它们系在一起。