需要获取TeamScores中最大价值的索引[],并用TeamNames的匹配索引打印关联的字符串[]

发布于 2025-02-13 19:54:36 字数 2131 浏览 2 评论 0原文

需要获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2025-02-20 19:54:39

您可以创建一个以存储团队名称及其得分的课程。然后根据比较器对类对象的数组进行排序。另外,您无需使用两个扫描仪对象。

class Team
{
    public int score;
    public String name;
}

class SmithJustin_Asgn6 {
    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        
        System.out.print("Enter the number of teams would you like to enter data for: ");
        int teams = userInput.nextInt();
        
        Team[] teamArray = new Team[teams];
        
        for(int i = 0; i < teams; i++) {
            teamArray[i] = new Team();
            userInput.nextLine();
            System.out.println("\nTeam "+ (i) +":");
            System.out.println();
            System.out.print("Enter Team's name: ");
            teamArray[i].name = userInput.nextLine();
            System.out.print("Enter Team's score (400-1000): ");
            teamArray[i].score = userInput.nextInt();
            System.out.println();
        }
        userInput.close();
        
        Arrays.sort(teamArray, new Comparator<Team>() {
            @Override
            public int compare(Team o1, Team o2) {
                return Integer.compare(o1.score, o2.score);
            }
        });
        
        System.out.println();
        System.out.println("The high score is "+ teamArray[teams - 1].score +" by team " + teamArray[teams - 1].name + " and the low score is "+ teamArray[0].score +".");
    }

}

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.

class Team
{
    public int score;
    public String name;
}

class SmithJustin_Asgn6 {
    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        
        System.out.print("Enter the number of teams would you like to enter data for: ");
        int teams = userInput.nextInt();
        
        Team[] teamArray = new Team[teams];
        
        for(int i = 0; i < teams; i++) {
            teamArray[i] = new Team();
            userInput.nextLine();
            System.out.println("\nTeam "+ (i) +":");
            System.out.println();
            System.out.print("Enter Team's name: ");
            teamArray[i].name = userInput.nextLine();
            System.out.print("Enter Team's score (400-1000): ");
            teamArray[i].score = userInput.nextInt();
            System.out.println();
        }
        userInput.close();
        
        Arrays.sort(teamArray, new Comparator<Team>() {
            @Override
            public int compare(Team o1, Team o2) {
                return Integer.compare(o1.score, o2.score);
            }
        });
        
        System.out.println();
        System.out.println("The high score is "+ teamArray[teams - 1].score +" by team " + teamArray[teams - 1].name + " and the low score is "+ teamArray[0].score +".");
    }

}
我很OK 2025-02-20 19:54:39

@andrey 您的arrays.sort.sort是主要的culprit。您需要逻辑才能获得与高分索引相同的低分索引。

public static int lowScore(int[] teamScores, int lowIndex) {
    // Arrays.sort(teamScores);
    int low = teamScores[0];
    //logic to low score's index
    return lowIndex;
}

具有两个索引后,您可以轻松地使用它们的数组从各个数组中获取值。
在您的main方法中,您多次调用相同的方法,而不是可以

int lowIndex = 0;
highIndex = highScore(teamScores, highIndex);
lowIndex = lowScore(teamScores, lowIndex);
System.out.println();
System.out.println("The high score is " + teamScores[highIndex] + " by team " + teamNames[highIndex] + " and the low score is " + teamScores[lowIndex] + ".");

开始学习 stream 。它简单有趣;)。

int h1 = IntStream.range(0, teamScores.length)
                .reduce((i, j) -> teamScores[i] > teamScores[i] ? i : j)
                .getAsInt();
int lowScore = Arrays.stream(teamScores).min().getAsInt();
System.out.println("The high score is " + teamScores[h1] + " by team " + teamNames[h1]+ " and the low score is " + lowScore + ".");

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.

public static int lowScore(int[] teamScores, int lowIndex) {
    // Arrays.sort(teamScores);
    int low = teamScores[0];
    //logic to low score's index
    return lowIndex;
}

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 do

int lowIndex = 0;
highIndex = highScore(teamScores, highIndex);
lowIndex = lowScore(teamScores, lowIndex);
System.out.println();
System.out.println("The high score is " + teamScores[highIndex] + " by team " + teamNames[highIndex] + " and the low score is " + teamScores[lowIndex] + ".");

Start learning stream. Its easy and fun ;).

int h1 = IntStream.range(0, teamScores.length)
                .reduce((i, j) -> teamScores[i] > teamScores[i] ? i : j)
                .getAsInt();
int lowScore = Arrays.stream(teamScores).min().getAsInt();
System.out.println("The high score is " + teamScores[h1] + " by team " + teamNames[h1]+ " and the low score is " + lowScore + ".");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文