I have created a monopoly game, and the problem right now is that once I press "y" to buy a property, it prints out the previous lines again, rather than going straight through the loop.
if (board1[pos_l].getType().equals ("prop")) {
if (board1[pos_l].getAvail() == (true) && numClicks!=0) {
println (board1[pos_l].getName() + " is available for $" + board1[pos_l].getPrice() + ". Press y to buy.");
println("player 2 roll the die (a) if player 1 doesn't buy it");
println("");
if (key == 'y' ){
board1 [pos_l].setAvail(false);
board1[pos_l].setOwner (name1);
p1money = p1money - board1[pos_l].getPrice();
println("you have bought it! you now have " +p1money + ". player 2 press 'a' to roll the die.");
numClicks=0;
}
}
I expect the output to be "you have bought it! you now have $___ .....". Instead, it's printing out "board1[pos_l].getName() + " is available for $" + board1[pos_l].getPrice() + Press y to buy. player 2 roll the die (a) if player 1 doesn't buy it" again after I press y.
You're not telling it not to print out the lines when the user presses Y. You need to move those lines to an else
block:
if (board1[pos_l].getAvail() == (true) && numClicks!=0) {
if (key == 'y' ){
board1 [pos_l].setAvail(false);
board1[pos_l].setOwner (name1);
p1money = p1money - board1[pos_l].getPrice();
println("you have bought it! you now have " +p1money + ". player 2 press 'a' to roll the die.");
numClicks=0;
} else {
println (board1[pos_l].getName() + " is available for $" + board1[pos_l].getPrice() + ". Press y to buy.");
println("player 2 roll the die (a) if player 1 doesn't buy it");
println("");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.