Every time this program loops, it resets the cash variable back to 500. How do I initially set it to 500, subtract the bet and then have it remember that value on its next iteration? My output for example:
Computer 1 bets: $50 I now have: $450 //
Computer 2 bets: $50 I now have: $450 //these are right
Computer 3 bets: $50 I now have: $450 //
Computer 1 bets: $50 I now have: $450 //
Computer 2 bets: $50 I now have: $450 //now should have $400
Computer 3 bets: $50 I now have: $450 //
Computer 1 bets: $50 I now have: $450 //
Computer 2 bets: $50 I now have: $450 //now should have $350
Computer 3 bets: $50 I now have: $450 //
My code:
public class Computer {
private int id;
private int bet;
private int cash = 500;
private static Computer[] c;
public static void create(int numComps) {
c = new Computer[numComps];
for (int i = 0; i < numComps; i++) {
c[i] = new Computer();
c[i].id = i + 1;
c[i].bet = 50;
c[i].cash -= c[i].bet;
c[i].display();
}
}
private void display() {
String name = "Computer " + id;
System.out.println(name + " bets: $" + bet + " I now have: $" + cash);
}
public static void main(String[] args) {
int i = 0;
do {
create(3);
System.out.println();
i++;
} while (i < 3);
}
}