在 keyPressed 之后打印一行两次

printing out a line twice after keyPressed

我创建了一个大富翁游戏,现在的问题是,一旦我按 "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;
              }
            }

我希望输出为 "you have bought it! you now have $___ ....."。相反,它打印出 "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 " 在我按 y 后再次出现。

你没有告诉它不要在用户按 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("");
  }
}