【问题标题】:I need to loop through an arrayList in Java and multiply the returned results together. I am struggling with the multiplying of the returned results我需要遍历 Java 中的 arrayList 并将返回的结果相乘。我正在努力与返回的结果相乘
【发布时间】:2017-10-28 10:36:08
【问题描述】:

我是 Java 新手,我的 Loops 正在苦苦挣扎。我想创建一个分析投注结果的基本程序。我的 Fixtures 类构造函数:

public Fixtures(String homeTeamName,double homeTeamOdds,String 
     awayTeamName, double awayTeamOdds,double drawOdds,String result) {
  this.homeTeamName=(homeTeamName);
  this.homeTeamOdds=(homeTeamOdds);
  this.awayTeamName=(awayTeamName);
  this.awayTeamOdds=(awayTeamOdds);
  this.drawOdds=(drawOdds);
  this.result=(result);
}

我已经创建了一个 Fixture 对象的 ArrayList:

Fixtures fixture2= new Fixtures("ManchesterUnited",1.6,"Spurs",3.1,2.8,"home");
Fixtures fixture3= new Fixtures("Manchester Citeh",1.3,"Burnley",4.1,3.8,"home");
Fixtures fixture4= new Fixtures("Newcastle",2.1,"Bomouth",4.1,2.6,"away");
Fixtures fixture5= new Fixtures("Everton",2.1,"Watford",4.6,2.3,"away");
Fixtures fixture6= new Fixtures("Chelsea",2.1,"Brighton",4.1,3.8,"draw");

ArrayList<Fixtures> games = new ArrayList<Fixtures>();

games.add(fixture2);
games.add(fixture3);
games.add(fixture4);
games.add(fixture5);
games.add(fixture6);

现在我想遍历 ArrayList 并找到结果为“主场”获胜的夹具,并将主场获胜几率相乘。我在下面打印出 2 个 homeTeamOdds。然后我想将它们相乘。我被卡住了。对于完成此循环的任何帮助将不胜感激

double x=0;
for (int i = 0; i < games.size(); i++){
    if (games.get(i).getResult().equals("home")){
        x=(games.get(i).getHomeTeamOdds());

        System.out.println(x);
    }

【问题讨论】:

    标签: java loops arraylist


    【解决方案1】:

    您需要将变量初始化为1,因为乘以 0 没有意义。您对“家”的检查没问题,只需将getHomeTeamOdds() 的结果与x 相乘即可:

    double x = 1;
    for (int i = 0; i < games.size(); i++) {
        if (games.get(i).getResult().equals("home")) {
            x *= games.get(i).getHomeTeamOdds();
        }
    }
    

    除了使用 ol' skool 方法,您还可以使用 Java Streams API:

    double d = games.stream()
        .filter(t -> Objects.equals(t.getResult(), "home"))
        .mapToDouble(t -> t.getHomeTeamOdds())
        .reduce(1, (a, b) -> a * b);
    

    在我看来,你的操作意图更明确。

    • .filter(t -&gt; Objects.equals(t.getResult(), "home")) 过滤结果为“home”的所有元素,丢弃所有其他元素;
    • .mapToDouble(t -&gt; t.getHomeTeamOdds()) 选择所有 homeTeamOdds 值;
    • .reduce(1, (a, b) -&gt; a * b) 对收集的值进行数学归约,以 1 作为初始值,归约操作为 lambda 表达式:将每个元素与之前的结果相乘。

    【讨论】:

    • 谢谢。我必须看看 Java Streams API。我以前从未使用过。
    【解决方案2】:

    这应该会给你你想要的

        Fixtures fixture2 = new Fixtures("ManchesterUnited", 1.6, "Spurs", 3.1, 2.8, "home");
        Fixtures fixture3 = new Fixtures("Manchester Citeh", 1.3, "Burnley", 4.1, 3.8, "home");
        Fixtures fixture4 = new Fixtures("Newcastle", 2.1, "Bomouth", 4.1, 2.6, "away");
        Fixtures fixture5 = new Fixtures("Everton", 2.1, "Watford", 4.6, 2.3, "away");
        Fixtures fixture6 = new Fixtures("Chelsea", 2.1, "Brighton", 4.1, 3.8, "draw");
    
        ArrayList<Fixtures> games = new ArrayList<Fixtures>();
    
        games.add(fixture2);
        games.add(fixture3);
        games.add(fixture4);
        games.add(fixture5);
        games.add(fixture6);
    
        double x = 1;
        for (Fixtures game : games)
            if (game.getResult().equals("home")) {
                x *= game.getHomeTeamOdds();
            }
    
        System.out.print(x);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2021-11-14
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      相关资源
      最近更新 更多