gpt4 book ai didi

java - 数组保龄球得分

转载 作者:行者123 更新时间:2023-12-02 03:07:35 25 4
gpt4 key购买 nike

当我运行代码时,它会输出附加信息。

但是,我给我的教授发了电子邮件,他说“看起来你的循环正在从文件中提取更多数据,你的 printArray 方法缺少告诉该方法数组中有多少个有效值的参数。相反,它应该是” printArray(blue_members, blue_scores, n); "其中 n 是数组中有效元素的数量"

在我向他展示了我的一些修复方法后,他回答道:“不!!!!你无法理解这个概念。数组的最大容量应该是 10,printArray 方法应该将每个数组中的有效元素作为参数。数组, teamb 和 teamw 整数对象跟踪数组中的许多有效元素”现在我是一个视觉人,我不明白他在问什么。

我尝试改变

String[] blue_members = new String[10], white_members = new String[10]; 
int[] blue_scores = new int[10], white_scores = new int[10];

String[] blue_members = new String[3], white_members = new String[3]; 
int[] blue_scores = new int[3], white_scores = new int[3];
import java.io.*;
import java.util.*;

// declaration of the class
public class Bowling7

{

public static void main(String[] args) throws FileNotFoundException
{
// 1. connect to input file
Scanner fin = new Scanner(new FileReader("bowling.txt"));

// declare arrays below
String Team, Member;
int teamw, teamb, Score;
String[] blue_members = new String[10], white_members = new String[10];
int[] blue_scores = new int[10], white_scores = new int[10];

// 2) initialize array accumulators to zero
int teamblue = 0;
int teamwhite = 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.equals("Blue")) {


blue_members[teamblue] = Member;
blue_scores[teamblue] = Score;


teamblue++;
} else {
white_members[teamwhite] = Member;
white_scores[teamwhite] = Score;

teamwhite++;
}
}

if(sumArray(blue_scores) > sumArray(white_scores))
{
printArray("Blue", blue_members, blue_scores);

} else {
printArray("White", white_members, white_scores);
}
// 7) then store blue member and score
// 8) increase blue array accumulator
// 9) else store white member and score
// 10) increase white array accumulator

fin.close();

}


public static int sumArray(int[] Score) {
int sum = 0;
for (int i = 0; i < Score.length; i++)
sum += Score[i];
return sum;
}

public static void printArray(String Team, String[] Member, int[] Score) {
System.out.println("Winning team:" + Team +"\n");
System.out.println("Player Score" + "\n");
for (int i = 0; i < Member.length; i++) {
System.out.printf(Member[i] + ":" + Score[i] + "\n");
}
}
}

保龄球。 txt 的组成为

Harry Blue 35
Tony White 43
Hilda Blue 92
Paul White 34
Tom White 20

输出

This program reads the lines from the file bowling.txt to determine
the winner of a bowling match. The winning team, members and scores
are displayed on the monitor.

Winning team:Blue

Player Score

Fred:20
Harry:35
Hilda:92
null:0
null:0
null:0
null:0
null:0
null:0
null:0

(我不应该得到 null:0)

最佳答案

该数组的最大容量为 10,因此其 length是 10。但是,它仅被部分填充,其余值为 null s。

因为您使用最大容量值循环遍历数组,所以您得到 null输出上有 s。

您需要添加length参数到您的 printArray()方法来表示实际存在的多个元素,并使用它代替 Member.length :

public static void printArray(String Team, String[] Member, int[] Score, int length) {
System.out.println("Winning team:" + Team +"\n");
System.out.println("Player Score" + "\n");
for (int i = 0; i < length; i++) {
System.out.printf(Member[i] + ":" + Score[i] + "\n");
}
}

然后,以适当的团队长度调用此方法,例如:

printArray("Blue", blue_members, blue_scores, teamblue);

关于java - 数组保龄球得分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57015169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com