【问题标题】:Comparing Strings and Integers in an Array at the same time同时比较数组中的字符串和整数
【发布时间】:2020-10-15 19:27:26
【问题描述】:

我正在做一个练习,要求我读取 csv 格式的文件,并询问用户关于他希望程序查找的单词的输入。这是文档的格式,索引 0 和 1 是球队的名称,因此字符串和索引 2 和 3 是比赛的比分:

ENCE,Vitality,9,16
ENCE,Vitality,16,12
ENCE,Vitality,9,16
ENCE,Heroic,10,16
SJ,ENCE,0,16
SJ,ENCE,3,16
FURIA,NRG,7,16
FURIA,Prospects,16,1

起初,练习要求我编写一个程序来读取文档并打印某支球队打了多少场比赛。现在它要我写一个比较分数并打印该特定团队的输赢总数。我试图用一百万种不同的方式来做,有没有一种有效的方法来同时比较字符串和整数?

我的代码如下:

import java.nio.file.Paths;

import java.util.Scanner;

public class SportStatistics {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("File:");
        String file = scan.nextLine();

        System.out.println("Team:");
        String team = scan.nextLine();

        try ( Scanner reader = new Scanner(Paths.get(file))) {
            int totalGames = 0;
            int teamPoints = 0;
            int otherPoints = 0;
            int wins = 0;
            int losses = 0;

            while (reader.hasNextLine()) {

                String info = reader.nextLine();

                if (info.isEmpty()) {
                    continue;
                }
                String[] parts = info.split(",");
                String homeN = parts[0];
                String visitorN = parts[1];
                int homeP = Integer.valueOf(parts[2]);
                int visitorP = Integer.valueOf(parts[3]);

                for (String part : parts) {
                    if (part.equals(team)) {
                        totalGames++;
                        
                    }
                    
                    if(homeN.equals(team)){
                        teamPoints = homeP;
                        otherPoints = visitorP;
                         if(teamPoints > otherPoints){
                            wins ++;
                        }else{
                             losses ++;
                         }
                        
                    }
                    
                    if(visitorN.equals(team)){
                        teamPoints = visitorP;
                        otherPoints = homeP;
                         if(teamPoints > otherPoints){
                            wins ++;
                        }else{
                             losses ++;
                         }
                        
                    }

                }

            }
            System.out.println("Games: " + totalGames);
            System.out.println(wins);
            
        } catch (Exception e) {

        }

    }
}

【问题讨论】:

    标签: java arrays io


    【解决方案1】:

    (这是使用数据库更有意义的问题)

    首先,你想要的是一个游戏类

    public class Game {
      String team1;
      String team2;
      int team1Score, team2Score;
    
      public Game(String t1, String t2, int score1, int score2);
    }
    

    现在你想制作一个游戏集合(可能是一个列表)

    List<Game> games = new ArrayList<>();
    

    然后你想将所有游戏添加到列表中

    那么你有一个选择。您可以创建一个 Map 并为您的所有团队创建列表,其中包括他们玩过的所有游戏。或者你可以制作一个方法,比如

    public List<Game> getGamesForTeam(String teamName, List allGames) {
      ...
    }
    

    提取包含团队teamName

    的游戏列表

    希望这对您有意义并且可以帮助您入门

    【讨论】:

    • 我还没有学过数据库,所以要坚持我所知道的。让我看看我是否理解正确。创建一个 Game 类,然后使用 split 方法在 ArrayList 中创建 Game 类的新对象。然后创建一些能够提取我需要的信息的方法。我想我明白了。谢谢。
    • @Nicolearagao 听起来你已经掌握了
    • 我想不通。现在我无法访问我在 Class Game 中创建的方法。我认为这是因为 toString 方法,因为我只看到了哈希值,但我修改了它,仍然无法访问我的 getter 和 setter。如果你能引导我朝着正确的方向前进,我会将我的代码添加到答案中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2013-09-03
    • 2015-10-27
    相关资源
    最近更新 更多