Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 满足条件时如何比较整数_Java_Arrays - Fatal编程技术网

Java 满足条件时如何比较整数

Java 满足条件时如何比较整数,java,arrays,Java,Arrays,我有一个程序,它读取一个文本文件,其中包含足球队的进球数。我希望能够扫描某个球队的文本文件,然后计算出一些东西,比如进球、入狱等等 public void totalGames() { File inputfile = new File ("validtest.txt.txt"); try{ Scanner filescan = new Scanner(inputfile); //Map<Str

我有一个程序,它读取一个文本文件,其中包含足球队的进球数。我希望能够扫描某个球队的文本文件,然后计算出一些东西,比如进球、入狱等等

public void totalGames()
    { 


        File inputfile = new File ("validtest.txt.txt");
        try{

            Scanner filescan = new Scanner(inputfile);
            //Map<String[], int[]> teams = new Map<>();
            HashMap<String,String> teamMap = new HashMap<String, String>();

            while(filescan.hasNext()){
            //  StrArray = aLine.split(separator);


                String[] teamKey = filescan.nextLine().split(" : ");


                teamMap.put(teamKey[0], teamKey[1]);





                }
            filescan.close();
            System.out.println(teamMap);

                }catch (FileNotFoundException e){


                }
                        I have managed to add only the teams to the map but it is making the teams equals to each other where as the team needs to be associated with the goals scored in that game.

Fulham : Liverpool : 1 : 2
Wigan Athletic : Leeds United : 2 : 2
Arsenal : Liverpool : 2 : 2
数据模型是关键词

我建议创建一个数据模型

找到新线 解析到模型 将模型添加到arraylist 检索用户的输入 检查是否存在与用户输入值完全相同的模型 如果是,则从模型中检索其他信息
如果用户输入团队名称,请尝试通过contains和indexOf方法查找包含完全相同团队的模型。

您可以执行以下操作:

创建具有以下详细信息的新类: 团队名称 反对 目标 其他目标 将“与对手名称相等”字段定义为要匹配的关键属性 从文件中读取数据并将其存储在地图中,其中key作为teamName,value作为具有上述详细信息的对象列表。 从用户处读取输入的团队名称,然后转到地图,从列表中获取团队详细信息,并将其显示给用户。
你需要的是将每支球队的进球数与他们的得分联系起来。数据结构就是您所需要的

在您的情况下,将所有团队作为关键存储在地图中,并为每个团队累积得分。尝试一下,如果你有任何问题,请编辑你的问题

编辑:


您只需创建一个新的游戏或Match类,然后循环浏览您的文件并将比赛详细信息存储在列表中:

然后将每行的匹配细节存储在匹配实例中,然后将其添加到匹配列表中

您的显示器的外观如下所示:

System.out.println(match.team+":"+match.opponent+" "+match.score+":"+match.opponentScore);
其中match表示匹配列表中的每个元素


我认为应该这样做。

富勒姆这个赛季又是垃圾。他们在对利物浦的比赛中永远不会进球,即使是在家里。看看MapA双地图的概念,即键->值和值->键加上修饰以防止键与值相等,只是为了进行额外的安全检查就足够了。你能解释一下你将“与对手名称字段相等”定义为要匹配的关键属性是什么意思吗?你如何将目标与团队相关联?我不确定如何拆分行以将整数与正确的字符串放在一起。我尝试过这种方法,但在将teamkey[3]分配给team2goals时失败了?问题是什么?也许您无法将2解析为字符串?找到删除这些空白的方法。
public Class Match{
 String team;
 int score:
 String opponent;
 int opponentScore;
}
System.out.println(match.team+":"+match.opponent+" "+match.score+":"+match.opponentScore);