我关于密码的第一个循环不起作用

问题:由于某种原因,循环 while (!input.equalsIgnoreCase(passWord))不起作用。即使密码不正确,执行也会直接进入另一个循环。如果有人可以帮助我,我将不胜感激。提前致谢。


public static void main(String[] args) {

    final int MAX_PLAYERS = 15 ;

    final int MIN_PLAYERS = 9 ;

    int players ;

    int teamSize ;

    int teams ;

    int leftOver ;

    String input ;


    String passWord = "PROSPERO" ;


    input = JOptionPane.showInputDialog(" What is the password please ?");

    passWord = String.valueOf(input);


    while (!input.equalsIgnoreCase(passWord))

    {

        input = JOptionPane.showInputDialog(" What is the password please ?");

        passWord = String.valueOf(input);

    }


    input = JOptionPane.showInputDialog("Enter the number of players per team :");

    teamSize = Integer.parseInt(input) ;


    while (teamSize < MIN_PLAYERS || teamSize > MAX_PLAYERS)

    {

        input = JOptionPane.showInputDialog("Sorry the minimum player" +

                " should be at least" + MIN_PLAYERS + " a maximum of " + MAX_PLAYERS) ;

        teamSize = Integer.parseInt(input) ;

    }


    input = JOptionPane.showInputDialog(" Enter the number of players :") ;

    players = Integer.parseInt(input) ;


    while ( players < 0 )

    {

        input = JOptionPane.showInputDialog(" Sorry the numbers of players should be greater than 0 . "

                + " Enter again a number : " ) ;

        players = Integer.parseInt(input) ;

    }

    teams = players / teamSize ;

    leftOver = players % teamSize ;


    JOptionPane.showMessageDialog(null, " There will be " + teams +

            "teams" + leftOver + " players leftover" + passWord);

}


鸿蒙传说
浏览 174回答 2
2回答

郎朗坤

问题就在这里,所以你输入的密码等于密码。input = JOptionPane.showInputDialog(" What is the password please ?");passWord = String.valueOf(input);你需要像这样修改它:input = JOptionPane.showInputDialog(" What is the password please ?");String inputPassWord = String.valueOf(input);while (!inputPassWord.equalsIgnoreCase(passWord)){&nbsp; &nbsp; input = JOptionPane.showInputDialog(" What is the password please ?");&nbsp; &nbsp; inputPassWord = String.valueOf(input);}

慕标琳琳

您正在使用password包含实际密码的相同变量。下面的逻辑应该可以解决你的问题,String passWord = "PROSPERO" ;String inputPassword="";inputPassword = JOptionPane.showInputDialog(" What is the password please ?");while (!inputPassword.equalsIgnoreCase(passWord)) {&nbsp; &nbsp; &nbsp; inputPassword = JOptionPane.showInputDialog(" What is the password please ?");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java