如何使while循环工作不一次? java的

时间:2015-09-05 21:33:13

标签: java loops if-statement while-loop

有人可以帮助我吗? 我正在学习java并且有这样的代码:

    // Method to Display special tests performed in the STData file
    
public void display(View view)
{
    
    
        
    // User SharedPreferences to save the value per special name    
    SharedPreferences sharedPref = getSharedPreferences("STData", Context.MODE_PRIVATE);
    
        
    shoulderSpinner.setSelection(sharedPref.getInt("spinnerSelection",0));
        

    //save special test Value in String 'testValue' <= entered by the PT
        
    String testValue = sharedPref.getString("STValue", DEFAULT);
    
    
        

    // DISPlAY THE VALUES ON THE SCREEN OF ACTIVITY !!!!


        // Test Name
        
    STNameTextView = (TextView) findViewById(R.id.STView3);
         
    STNameTextView.setText(testName);
    
        // Name of the Special Test
         
    STValueTextView = (TextView) findViewById(R.id.STView4);
         
    STValueTextView.setText(testValue);
    }

}

我需要一个“while循环”:当 //... 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(); } } } (!d.getPlayerName().equals(chose))循环工作到(d.getPlayerTalon()>0).

但是现在程序在d.getPlayerTalon()>0的第一圈之后停止了,但我需要这么多圈子(!d.getPlayerName().equals(chose))直到(!d.getPlayerName().equals(chose))

团队中有3名玩家,每个玩家有10张票,例如我想选择1号玩家并从他那里拿一张票 - 所以现在他有9张票,之后我想再次检查是团队中的第一名球员?如果有这样的球员,我会检查他是否有门票,如果是的话, - 再拿一张票,所以#1球员将有8张门票,依此类推。

对不起我的解释,但我会尽力解释。

2 个答案:

答案 0 :(得分:0)

你的代码中有2个块 - 一个是do-while循环,另一个是:

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

是的,您可以在Java中使用没有与它们关联的控制结构的块,希望这可以澄清一点。

答案 1 :(得分:0)

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.