首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据用户输入从数组中删除元素?

如何根据用户输入从数组中删除元素?
EN

Stack Overflow用户
提问于 2021-08-23 05:38:30
回答 1查看 55关注 0票数 0

新手来了。基本上,我想从数组中删除用户选择的元素。

但我不知道如何首先删除它。这是代码。

代码语言:javascript
复制
 static void TeamFunction() {

        Scanner scan = new Scanner(System.in);
        int teamNo;
        System.out.println("-----Teams------");
        do {
            System.out.println("Enter Number of teams '(max-4)' and '(min-2)' ");
            teamNo = scan.nextInt();
        } while (teamNo > 4 || teamNo == 1);

        String[] teamName = new String[teamNo];
        int[] teamScore = new int[teamNo];
        String[] Tevent = new String[5];
        int teamRank;
        int eventNo = 5;
// condition check for number of teams
        // skip all of team code if 0

        if (teamNo == 0) {
            System.out.println("--Skipped-Teams--");
        } else {
            // event names
            for (int i = 0; i < 5; i++) {
                System.out.println("Enter Event Name " + (i + 1) + " for the teams");
                Tevent[i] = scan.next();
            }
// name and rank of the teams

            for (int i = 0; i < teamNo; i++) {
                System.out.println("Enter Name of team " + (i + 1));
                teamName[i] = scan.next();
            }

            for (int a = 0; a < eventNo; a++) {
                for (int i = 0; i < teamNo; i++) {
                    System.out.println("Enter rank of the team " + (teamName[i]) + " on the event " + (a + 1));
                    teamRank = scan.nextInt();
                    int tRank = 0;
                    // scoring system for the teams
                    switch (teamRank) {
                        case 3:
                            tRank = 5;
                            break;
                        case 2:
                            tRank = 10;
                            break;
                        case 1:
                            tRank = 20;
                            break;
                    }
                    if (teamRank == 0 || teamRank >= 4) {
                        System.out.println("The team will not be awarded points");
                    } else {
                        teamScore[i] += tRank;
                        System.out.println(tRank + " points is granted for this event\n");
                    }

                    if (scan.hasNextLine()) {
                        scan.nextLine();
                    }
                }
                int topTScore = 0; // this will be used to find the highest value score
                int topteam = -1; // and this will be the index of the team
                for (int i = 0; i < teamNo; i++) {
                    if (teamScore[i] > topTScore) {
                        topTScore = teamScore[i]; // the highest score (up to now)
                        topteam = i; // save the team number for the one with the highest score
                    }
                }
                System.out.println(teamName[topteam] + " is the winner, with a score of " + topTScore+"\n");

                System.out.println("Enter the team number who you wish to remove after event 1\n");
                if (teamNo == 2) {
                    System.out.println("Team 1 is 0");
                    System.out.println("Team 2 is 1\n");
                }
                if (teamNo == 3) {
                    System.out.println("Team 1 is 0");
                    System.out.println("Team 2 is 1");
                    System.out.println("Team 3 is 2\n");
                }
                if (teamNo ==4) {
                    System.out.println("Team 1 is 0");
                    System.out.println("Team 2 is 1");
                    System.out.println("Team 3 is 2");
                    System.out.println("Team 4 is 3\n");
                }

                int removeIndex = scan.nextInt();

// and remove the element that the user entered. <--- this is the one . i dont know where to start.
                
                }
            }
EN

回答 1

Stack Overflow用户

发布于 2021-08-23 05:48:52

您正在使用一个原始类型为int的数组。java中的数组不提供remove函数。这意味着,真正删除元素而不只是将元素设置为-1或0的唯一方法是创建一个新数组并复制相关数据。

您可能还想签出ArrayLists,因为它有一个允许您在索引处删除的删除函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68887768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档