简体   繁体   English

在keyPressed之后打印两次

[英]printing out a line twice after keyPressed

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. 我创建了一个垄断游戏,现在的问题是,一旦我按“ 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;
              }
            }

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. 相反,它打印出“ board1 [pos_l] .getName()+“可用于$” + board1 [pos_l] .getPrice()+按y进行购买。如果玩家1没有,玩家2掷骰子(a)按下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: 您并不是要告诉它在用户按下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("");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM