简体   繁体   English

如何让while循环不只工作一次? 爪哇

[英]how to make while loop work not once? java

Could someone help me?有人可以帮助我吗? I'm studying java and have such code:我正在学习java并且有这样的代码:

   //...
        for (int i = 0; i < player.length; i++) {
            Player d = player[i];
            Object chose;
            do {
                String chosenplayer = JOptionPane.showInputDialog("Please choose a player:");
                System.out.println("you chose player: " + chosenplayer);
                chose = chosenplayer;

            } while (!d.getPlayerName().equals(chose)) {
                if (d.getPlayerTalon() > 0) {
                    System.out.println("name=" + d.getPlayerName() + " talon=" + d.getPlayerTalon());
                    d.setFreeTalons();
                }
            }
        }

I need a "while loop": when (!d.getPlayerName().equals(chose)) and (d.getPlayerTalon()>0).我需要一个“while 循环”:when (!d.getPlayerName().equals(chose))(d.getPlayerTalon()>0). loop works till d.getPlayerTalon()>0循环工作直到d.getPlayerTalon()>0

but now the program stops after first circle of (!d.getPlayerName().equals(chose)) but I need such many circles with (!d.getPlayerName().equals(chose)) till d.getPlayerTalon()>0但现在程序在(!d.getPlayerName().equals(chose))第一个圆圈后停止,但我需要这么多圆圈(!d.getPlayerName().equals(chose))直到d.getPlayerTalon()>0

there is 3 players in a team, each player has 10 tickets, For example I would like to choose player#1 and take one ticket from him - so now he has 9 tickets, after that I would like to check again is a player#1 in the team?一队有3名球员,每个球员有10张票,比如我想选择球员#1,从他那里拿一张票——所以现在他有9张票,然后我想再检查一下是不是球员# 1 在团队中? if there is such player I would to check has he a tickets, if yes, - take one more ticket, so the player#1 will have 8 tickets, and so on.如果有这样的球员,我会检查他是否有票,如果是, - 再拿一张票,所以玩家#1 将有 8 张票,依此类推。

Sorry for my explanation, but I do my best to explain it.抱歉我的解释,但我会尽力解释。

You have 2 blocks in your code - one is the do-while loop, the other is this:您的代码中有 2 个块 - 一个是 do-while 循环,另一个是这样的:

       { 
         if (d.getPlayerTalon()>0){
               System.out.println (...);
             d.setFreeTalons();
          }
       }

Yes, you can have blocks without control structures associated with them in Java, hope this clarifies it a bit.是的,您可以在 Java 中拥有没有与之关联的控制结构的块,希望这能澄清一点。

for (int i = 0; i < player.length; i++) {
            Player d = player[i];
            String chose;
            do {
                String chosenplayer = JOptionPane
                        .showInputDialog("Please choose a player:");
                System.out.println("you chose player: " + chosenplayer);
                chose = chosenplayer;

            } while (!d.getPlayerName().equals(chose));
            if (d.getPlayerTalon() > 0) {
                System.out.println("name=" + d.getPlayerName() + " talon="
                        + d.getPlayerTalon());
                d.setFreeTalons();
            }

        }
    }

Use this code.

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

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