我需要在Java中循环一个arrayList,并将返回的结果相乘。我正在努力使返回的结果成倍增加

我需要在Java中循环一个arrayList,并将返回的结果相乘。我正在努力使返回的结果成倍增加,java,loops,arraylist,Java,Loops,Arraylist,我是Java新手,正在努力解决循环问题。我想创建一个基本程序来分析下注结果。我的Fixtures类构造函数: public Fixtures(String homeTeamName,double homeTeamOdds,String awayTeamName, double awayTeamOdds,double drawOdds,String result) { this.homeTeamName=(homeTeamName); this.homeTeamOdds=(ho

我是Java新手,正在努力解决循环问题。我想创建一个基本程序来分析下注结果。我的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);
}
我已经创建了设备对象的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);
Fixtures fixture2=新装置(“ManchesterUnited”,1.6,“Spurs”,3.1,2.8,“home”);
固定装置fixture3=新固定装置(“曼彻斯特城”,1.3,“伯恩利”,4.1,3.8,“家”);
固定装置fixture4=新固定装置(“纽卡斯尔”,2.1,“博茅斯”,4.1,2.6,“客场”);
固定装置fixture5=新固定装置(“埃弗顿”,2.1,“沃特福德”,4.6,2.3,“客场”);
固定装置固定装置6=新固定装置(“切尔西”,2.1,“布莱顿”,4.1,3.8,“平局”);
ArrayList games=新建ArrayList();
游戏。添加(固件2);
游戏。添加(固件3);
游戏。添加(固件4);
游戏。添加(固件5);
游戏。添加(固件6);
现在,我想循环浏览ArrayList,找到结果是“主场”获胜的赛程,并将主场获胜几率相乘。下面的内容将打印出2个主场获胜几率。我想把它们相乘。我被卡住了。如果能帮我完成这个循环,我将不胜感激

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);
    }
double x=0;
对于(int i=0;i
您需要将变量初始化为
1
,因为与0相乘没有意义。您对“home”的检查没有问题,只需将
GetHomeTeamOffics()
的结果与
x相乘即可:

double x = 1;
for (int i = 0; i < games.size(); i++) {
    if (games.get(i).getResult().equals("home")) {
        x *= games.get(i).getHomeTeamOdds();
    }
}
在我看来,你们行动的意图更加明确

  • .filter(t->Objects.equals(t.getResult(),“home”)
    过滤结果为“home”的所有元素,丢弃所有其他元素
  • .mapToDouble(t->t.getHomeTeamOfficies())
    选择所有
    HomeTeamOfficies
  • .reduce(1,(a,b)->a*b)
    对收集的值应用数学约简,以1作为初始值,并将约简操作作为lambda表达式:将每个元素与之前的结果相乘

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

double x = 1;
for (int i = 0; i < games.size(); i++) {
    if (games.get(i).getResult().equals("home")) {
        x *= games.get(i).getHomeTeamOdds();
    }
}
在我看来,你们行动的意图更加明确

  • .filter(t->Objects.equals(t.getResult(),“home”)
    过滤结果为“home”的所有元素,丢弃所有其他元素
  • .mapToDouble(t->t.getHomeTeamOfficies())
    选择所有
    HomeTeamOfficies
  • .reduce(1,(a,b)->a*b)
    对收集的值应用数学约简,以1作为初始值,并将约简操作作为lambda表达式:将每个元素与之前的结果相乘

这会给你想要的

    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);
Fixtures fixture2=新装置(“ManchesterUnited”,1.6,“Spurs”,3.1,2.8,“home”);
固定装置fixture3=新固定装置(“曼彻斯特城”,1.3,“伯恩利”,4.1,3.8,“家”);
固定装置fixture4=新固定装置(“纽卡斯尔”,2.1,“博茅斯”,4.1,2.6,“客场”);
固定装置fixture5=新固定装置(“埃弗顿”,2.1,“沃特福德”,4.6,2.3,“客场”);
Fixtures fixture6=新赛程(“切尔西”,2.1,“布莱顿”,4.1,3.8,“平局”);
ArrayList games=新建ArrayList();
游戏。添加(固件2);
游戏。添加(固件3);
游戏。添加(固件4);
游戏。添加(固件5);
游戏。添加(固件6);
双x=1;
用于(游戏:游戏)
if(game.getResult().equals(“home”)){
x*=game.getHomeTeamOffics();
}
系统输出打印(x);

这会给你想要的

    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);
Fixtures fixture2=新装置(“ManchesterUnited”,1.6,“Spurs”,3.1,2.8,“home”);
固定装置fixture3=新固定装置(“曼彻斯特城”,1.3,“伯恩利”,4.1,3.8,“家”);
固定装置fixture4=新固定装置(“纽卡斯尔”,2.1,“博茅斯”,4.1,2.6,“客场”);
固定装置fixture5=新固定装置(“埃弗顿”,2.1,“沃特福德”,4.6,2.3,“客场”);
Fixtures fixture6=新赛程(“切尔西”,2.1,“布莱顿”,4.1,3.8,“平局”);
ArrayList games=新建ArrayList();
游戏。添加(固件2);
游戏。添加(固件3);
游戏。添加(固件4);
游戏。添加(固件5);
游戏。添加(固件6);
双x=1;
用于(游戏:游戏)
if(game.getResult().equals(“home”)){
x*=game.getHomeTeamOffics();
}
系统输出打印(x);

谢谢。我必须看看Java Streams API。我以前从未使用过它。谢谢。我必须看看Java Streams API。我以前从未使用过它。