Java 正在跳过System.out.println()
在第二个for循环没有出现之后,我的提示“请输入您的ID”,它直接转到“请输入您的密码”。它还跳过了从登录提示到密码提示的许多代码。如果你对它为什么会这样有任何想法,请与我分享,谢谢Java 正在跳过System.out.println(),java,Java,在第二个for循环没有出现之后,我的提示“请输入您的ID”,它直接转到“请输入您的密码”。它还跳过了从登录提示到密码提示的许多代码。如果你对它为什么会这样有任何想法,请与我分享,谢谢 public void main(String[] args){ Accounts Accounts = new Accounts(); Scanner kb = new Scanner(System.in); System.out.print("Please create a log
public void main(String[] args){
Accounts Accounts = new Accounts();
Scanner kb = new Scanner(System.in);
System.out.print("Please create a login ID and hit \"ENTER\": ");
Login = kb.nextLine();
for(;;){
if(!Accounts.isTaken(Login)){
System.out.print("Please create a password and hit \"ENTER\": ");
PW = kb.nextLine();
if(Accounts.validPW(PW)){
Accounts.createAccount(Login, PW);
break;
}
}
}
System.out.print("Do you wish to Log in ? (Y/N): ");
String response = kb.nextLine();
if((response=="y")||(response=="Y")){
for(;;){
//Not Being Shown
System.out.println("Please enter your ID: "); // ?????? Where are you?????
Login = kb.nextLine();
Accounts.login(Login);
//Goes straight here
System.out.print("Please enter your Password: ");
if ((Accounts.isValid(Login))&&(Accounts.checkAuth(kb.nextLine()))){
break;
}
else{
System.out.println("Invalid Login-Password!");
}
}
}
System.out.println("Please enter your Password: ");
System.out.println("LOGIN AUTHORIZED: "+Accounts.checkAuth(kb.nextLine()));
}
而不是
(response=="y")||(response=="Y")
使用
您正在使用
==
运算符检查字符串是否相等。使用equals()方法
=
操作员:
equals()方法
if((response.equals("y"))||(response.equals("Y"))){
甚至更好
if((response.equalsIgnoreCase("y"))){
由于java是基于引用的,所以响应的id与“y”不同
response.equals("y")
可能重复使用
String.equals()
,而不是=
。不要将用于(;;)
循环——我们有while循环用于此…最好编写while(true)
而不是用于(;;)
@Charles。不客气。祝你好运。顺便说一句,如果回答有帮助,请不要忘记接受它。
response.equals("y")