【发布时间】:2014-01-17 02:09:20
【问题描述】:
下面是我编写的一个简单程序,它会要求输入密码。如果我输入了错误的密码,系统会提示我“密码不正确,你想再试一次吗?”,如果我说不或者其他不以'y'开头的东西,它将终止程序。问题是,如果我输入正确的密码“Noah”,它会显示“密码正确”并再次循环返回“输入密码”。输入正确密码后,如何让这个程序终止?谢谢。
import java.util.Scanner;
public class methods
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String response = "yes";
System.out.println("Enter password:");
while(response.charAt(0)=='y')
{
String input = sc.nextLine();
if(input.equalsIgnoreCase("Noah")==true)
{
System.out.println("Password correct");
}
else if(input.equalsIgnoreCase("Noah")==false)
{
System.out.println("Password incorrect, would you like to try again?");
response = sc.nextLine();
}
}
}
}
【问题讨论】:
-
input.equalsIgnoreCase("Noah")==true去掉==true,看到这个我生气了。 -
也不需要“if(input.equalsIgnoreCase("Noah")==false)”,如果它不是真的,它是假的。
标签: java loops if-statement for-loop while-loop