为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

鸿蒙传说 2021-07-09 18:10:40
问题:由于某种原因,循环 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);}
查看完整描述

2 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

问题就在这里,所以你输入的密码等于密码。


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))

{

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

    inputPassWord = String.valueOf(input);

}


查看完整回答
反对 回复 2021-07-14
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

您正在使用password包含实际密码的相同变量。下面的逻辑应该可以解决你的问题,


String passWord = "PROSPERO" ;

String inputPassword="";


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


while (!inputPassword.equalsIgnoreCase(passWord)) {

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

}


查看完整回答
反对 回复 2021-07-14
  • 2 回答
  • 0 关注
  • 173 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信