0

作为初学者,我正在研究轮盘赌游戏,但我被卡住了。

我有一个选择的Bin对象

Bin(){
    outcomes = new TreeSet<Outcome>();
}

我有一个Bet对象

public Bet(int amount, Outcome outcome){
    this.outcome = outcome;
    this.amountBet = amount;
}

包含一个Outcome对象。

public Outcome(String name, int odds){
        this.name = name;
        this.odds = odds;
    }

目标 - 遍历 Bin 中的所有结果并将 Bin 中的 Outcome.name 与 bets.outcome.name 进行比较。如果我们有马赫,就会有胜利。如果没有,那就有损失。

所以,这是我的代码:

System.out.println(bin.toString());
System.out.println(table.bets.toString());

System.out.println(black.toString());

ListIterator<Bet> i = table.bets.listIterator();
    Iterator<Outcome> b = bin.outcomes.iterator();

    while(i.hasNext()) {

        while(b.hasNext()){


            if(i.next().outcome.equals(b.next())){

                System.out.println("Win!");

            }
            else System.out.println("Win :/");
        }


    }

问题:即使输出显示为:

[8 (35:1)]['7, 71' (17:1)]['8, 81' (17:1)][5, 53 (17:1)][8, 83 (17:1)][7 (11:1)][4, 41,    
43, 44 (8:1)][5, 51, 53, 54 (17:1)][7, 71, 73, 74 (8:1)][8, 81, 83, 84 (17:1)][4, 5,6,   
7, 8, 9 (5:1)][7, 8,9, 10, 11, 12 (5:1)][1 (2:1)][11 (2:1)][Low (1:1)][Even (1:1)]  

[Black (1:1)]

[10 on [Black (35:1)]]

Black (35:1)

No :/

Exception in thread "main" java.util.NoSuchElementException
at java.util.LinkedList$ListItr.next(Unknown Source)
at Roulette.Game.main(Game.java:37)

似乎它

a)不遍历 Bin 中的所有结果 b)当它找到匹配项时,它不会评估是否为真。

你能看到我做错了什么吗?

非常感谢你的帮助!!

如果文字太多或太少,我很抱歉。以防万一您需要查看其他课程中发生了什么,它们是:

游戏类 https://gist.github.com/anonymous/5473187

表类 https://gist.github.com/anonymous/5473188

投注类 https://gist.github.com/anonymous/5473189

结果类 https://gist.github.com/anonymous/5473191

Bin类 https://gist.github.com/anonymous/5473192

BinBuilder 类 https://gist.github.com/anonymous/5473197

车轮类 https://gist.github.com/anonymous/5473200

非随机类 https://gist.github.com/anonymous/5473202

乘客 57 类https://gist.github.com/anonymous/5473207

编辑:删除 System.out.println() 并更新了新结果。

4

2 回答 2

3

你打电话给每个i.next()b.next()两次。next()转到下一个元素,所以如果你有一个序列 1、2、3、4,你打印 1、3 并比较 2、4。

println()将结果复制到变量中以避免语句中出现这种意外的副作用:

ListIterator<Bet> i = table.bets.listIterator();
Iterator<Outcome> b = bin.outcomes.iterator();

while(i.hasNext()) {
  Bet bet = i.next();
  System.out.println(bet.outcome.name.toString());
  while(b.hasNext()){
    Outcome o = b.next();
    System.out.println(o.name.toString());

    if(bet.outcome.equals(o)){
      System.out.println("Win!");
    } else {
      System.out.println("Win :/");
    }
}

PS:

最干净的解决方案可能是避免使用迭代器并在相应的可迭代对象上使用 for 循环。

于 2013-04-27T14:01:48.950 回答
1

试试这个:

while(b.hasNext()){
    String str = b.next().name.toString()
    System.out.println(str);

    if(i.next().outcome.equals(str))
        System.out.println("Win!");

    else 
        System.out.println("Win :/");
}

代替 :

while(b.hasNext()){
    System.out.println(b.next().name.toString());

    if(i.next().outcome.equals(b.next())){    
        System.out.println("Win!");

    }
    else 
        System.out.println("Win :/");
}
于 2013-04-27T14:02:26.280 回答