需要获取TeamScores中最大价值的索引[],并用TeamNames的匹配索引打印关联的字符串[]
需要获取TeamScores []中最大价值的索引,并使用TeamNames []的匹配索引打印关联的字符串。这确实开始让我感到紧张。我已经能够成功地为打印的分数获得正确的价值,但它不断打印错误的团队。当我试图进行故障排除时,我得到了合适的团队,但得分错误。我绝对迷路了,没有其他想法。有人提供一些建议吗?我必须使用两个单独的数组,因此我不能仅将其简化为一个数组。我还必须使用for loop来检索值,所以我不能像对速率的情况一样做。
public class SmithJustin_Asgn6 {
public static int highScore(int[] teamScores, int highIndex) {
int max = teamScores[0];
for(int i = 0; i < teamScores.length; i++) {
if(max < teamScores[i]) {
max = teamScores[i];
highIndex = i;
}
}return highIndex;
}
public static int lowScore(int[] teamScores) {
Arrays.sort(teamScores);
int low = teamScores[0];
return low;
}
public static void main(String[] args) {
int highIndex = 0;
Scanner userInput=new Scanner(System.in);
Scanner scoreInput=new Scanner(System.in);
System.out.print("Enter the number of teams would you like to enter data for: ");
int teams=scoreInput.nextInt();
int [] teamScores= new int[teams];
String [] teamNames= new String[teams];
for(int i = 0; i < teams; i++) {
System.out.println("\nTeam "+ (i) +":");
System.out.println();
System.out.print("Enter Team's name: ");
String teamName=userInput.nextLine();
teamNames[i]=teamName;
System.out.print("Enter Team's score (400-1000): ");
int teamScore=scoreInput.nextInt();
teamScores[i]=teamScore;
System.out.println();
}
highScore(teamScores, highIndex);
lowScore(teamScores);
System.out.println();
System.out.println("The high score is "+ teamScores[highScore(teamScores, highIndex)] +" by team " + teamNames[highScore(teamScores, highIndex)] + " and the low score is "+ lowScore(teamScores) +".");
userInput.close();
scoreInput.close();
}
}
一直在尝试将其切成薄片,我完全被卡住了
Need to get the index of the largest value in teamScores[] and print the associated string with the matching index from teamNames[]. This is really starting to get on my nerves. I had been able to successfully get the right value for the scores printed but it kept printing the wrong team. When I was trying to troubleshoot I was getting the right team but the wrong score. I am absolutely lost and have no other ideas. Anybody offer some advice? I have to use two separate arrays so I cannot just reduce it to one array. I also have to use a for loop to retrieve the values, so I can't do like I did with the lowScore.
public class SmithJustin_Asgn6 {
public static int highScore(int[] teamScores, int highIndex) {
int max = teamScores[0];
for(int i = 0; i < teamScores.length; i++) {
if(max < teamScores[i]) {
max = teamScores[i];
highIndex = i;
}
}return highIndex;
}
public static int lowScore(int[] teamScores) {
Arrays.sort(teamScores);
int low = teamScores[0];
return low;
}
public static void main(String[] args) {
int highIndex = 0;
Scanner userInput=new Scanner(System.in);
Scanner scoreInput=new Scanner(System.in);
System.out.print("Enter the number of teams would you like to enter data for: ");
int teams=scoreInput.nextInt();
int [] teamScores= new int[teams];
String [] teamNames= new String[teams];
for(int i = 0; i < teams; i++) {
System.out.println("\nTeam "+ (i) +":");
System.out.println();
System.out.print("Enter Team's name: ");
String teamName=userInput.nextLine();
teamNames[i]=teamName;
System.out.print("Enter Team's score (400-1000): ");
int teamScore=scoreInput.nextInt();
teamScores[i]=teamScore;
System.out.println();
}
highScore(teamScores, highIndex);
lowScore(teamScores);
System.out.println();
System.out.println("The high score is "+ teamScores[highScore(teamScores, highIndex)] +" by team " + teamNames[highScore(teamScores, highIndex)] + " and the low score is "+ lowScore(teamScores) +".");
userInput.close();
scoreInput.close();
}
}
Been trying every way to slice it and I am completely stuck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个以存储团队名称及其得分的课程。然后根据比较器对类对象的数组进行排序。另外,您无需使用两个
扫描仪
对象。You can create a class to store team name and its score. Then sort the array of class objects based on a comparator. Also, you don't need to use two
Scanner
objects.如 @andrey 您的
arrays.sort.sort
是主要的culprit。您需要逻辑才能获得与高分索引相同的低分索引。具有两个索引后,您可以轻松地使用它们的数组从各个数组中获取值。
在您的
main
方法中,您多次调用相同的方法,而不是可以开始学习
stream
。它简单有趣;)。As mentioned by @Andrey your
Arrays.sort
is the main culprit. You need a logic to get the low score index the same as you have done for high score index.After you have both the indexes, you can easily get values from respective arrays using them.
In your
main
method you are calling the same methods multiple times instead of that you can doStart learning
stream
. Its easy and fun ;).