我班上有点不对劲。我是初学者

时间:2013-04-28 12:11:19

标签: java

我正在制作一个轮盘游戏,我正在测试它。

我有一个Game类,可以构建并准备好一切。它还构建了一个名为Passenger57的测试播放器,只能在黑色上下注。

不知何故,我无法让Game-class调用Passenger57并让它下注。 该表返回一个空的投注列表!

我在俯瞰什么?

GAME

package Roulette;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.TreeSet;

public class Game {

public static void main(String[] args) {
    Table table = new Table();
    BinBuilder bb = new BinBuilder();
    Passenger57 p57 = new Passenger57(table);
    p57.placeBets();    // <-- This guy is not doing much!
// ETC ETC//

PASSENGER57

package Roulette;

public class Passenger57 {

public Table table;
public Outcome black;
public Bet bet;

public Passenger57(Table table){
    this.table = new Table();
    this.black = new Outcome("Black", 1);

}

void placeBets(){
    Bet bet = new Bet(100, black);
    table.placeBet(bet);
}

 void win(Bet bet){
    System.out.println("You've won: " + bet.winAmount());
}

 void lose(Bet bet){
     System.out.println("You lost!"+bet.loseAmount()+ ":/");

}


}

非常感谢您帮助初学者磕磕绊绊!

万一你需要它

表格

package Roulette;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class Table {

public int limit = 1000;
public LinkedList<Bet> bets;

public Table(){
    bets = new LinkedList<Bet>();
}

public  boolean isValid(Bet bet){
    int sum = 0;
    for(Bet bett: bets){
        sum += bett.amountBet;
    }
    return (sum>limit);
}

public void placeBet(Bet bet){
        this.bets.add(bet);
}

ListIterator<Bet> iterator(){
    return bets.listIterator();

}
}

2 个答案:

答案 0 :(得分:8)

在课程 Passenger57 中,您应该更改

public Passenger57(Table table){
    this.table = new Table(); // <- here is your fault
    black = new Outcome("Black", 1); 
}

public Passenger57(Table table){
    this.table = table;
    black = new Outcome("Black", 1); 
}

答案 1 :(得分:2)

替换

this.table = new Table()

通过

this.table = table in Passenger57(Table table)