在keyPressed之后打印两次

时间:2019-05-17 19:33:08

标签: java processing

我创建了一个垄断游戏,现在的问题是,一旦我按“ y”购买物业,它就会再次打印出前几行,而不是直接循环。

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;
              }
            }

我希望输出为“您已经购买了!您现在有$ ___ .....”。相反,它打印出“ board1 [pos_l] .getName()+“可用于$” + board1 [pos_l] .getPrice()+按y进行购买。如果玩家1没有,玩家2掷骰子(a)按下y后再次购买。

1 个答案:

答案 0 :(得分:0)

您并没有要求用户按Y键打印出这些行。。您需要将这些行移动到else块中:

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("");
  }
}