Java 数组存储的值太多

Java 数组存储的值太多,java,arrays,for-loop,Java,Arrays,For Loop,该程序旨在将以前的输入与当前的输入进行比较。如果尚未输入输入,则输入将存储在团队的下一个索引中,并使用带值的同一索引进行存储。如果输入已输入,则该值将递增 注意:数组“score”被初始化为int。数组“team”被初始化为字符串。W for (j = 0; !input.equals("x"); j++ ) // problem with j making too many on the list { // moves onto a new System.out.p

该程序旨在将以前的输入与当前的输入进行比较。如果尚未输入输入,则输入将存储在团队的下一个索引中,并使用带值的同一索引进行存储。如果输入已输入,则该值将递增 注意:数组“score”被初始化为int。数组“team”被初始化为字符串。W

for (j = 0; !input.equals("x"); j++ )  // problem with j making too many on the list
    { // moves onto a new 
        System.out.println("Which team just won? (x to exit)"); 
        input = scnr.nextLine();
        for (i = 0; i < team.length && !input.equals("x"); i++) 
        { // compares input against previous inputs
            if (team[i].equals(input)) 
            { // comparison issue FIXME
                score[i]++; 
                break;
            }
            else if (!team[i].equals(input) && score[j] == 0) 
            {
                team[j] = input;
                score[j] = 1;
            }
        }
    }
(j=0;!input.equals(“x”);j++)的
for//j在列表中做得太多的问题
{//移动到一个新的
System.out.println(“哪支球队刚刚获胜?(x退出)”;
input=scnr.nextLine();
对于(i=0;i
这是您可以使用的另一种方法:

import java.util.Scanner;

class Scratch {
    public static final int MAX_SIZE = 1000;

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        String input;
        String[] team = new String[MAX_SIZE];
        int[] score = new int[MAX_SIZE];
        int currentTeamCount = 0;
        boolean teamAlreadyExists = false;

        System.out.println("Which team just won? (x to exit)");
        input = scnr.nextLine();
        while (!input.equalsIgnoreCase("x")) {
            teamAlreadyExists = false;

            for (int i = 0; i < currentTeamCount; i++) {
                if (input.equals(team[i])) {
                    score[i]++;
                    teamAlreadyExists = true;
                    break;
                }
            }

            if (!teamAlreadyExists) {
                team[currentTeamCount] = input;
                score[currentTeamCount] = 1;
                currentTeamCount++;
            }

            System.out.println("Which team just won? (x to exit)");
            input = scnr.nextLine();
        }

        for (int i = 0 ; i < currentTeamCount; i++) {
            System.out.println(team[i] + " " + score[i]);
        }
    }
}
输出:

A 1
B 2
C 2
D 3
E 1
F 2

希望有帮助:)

这是一个2D
阵列吗?不,我需要使用两个单独的阵列。你能发布
阵列的偏差吗?(我假设
score
是一个
int[]
team
是一个
String[]
?)“如果你需要进一步澄清,我会在评论中回答”如果你需要澄清问题,你需要编辑问题,这样读者就可以理解问题本身的优点。请勿仅在评论中添加澄清。感谢您的反馈。我对问题进行了编辑,使其更加清晰。这回答了我需要知道的所有问题。谢谢你的帮助。
A 1
B 2
C 2
D 3
E 1
F 2