问题:由于某种原因,循环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);
}
答案 0 :(得分:0)
您正在使用包含实际密码的相同变量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 ?");
}
。下面的逻辑应该可以解决您的问题,
{{1}}
答案 1 :(得分:0)
问题出在这里,因此您输入的密码等于密码。
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);
}