[英]Jump out of a while loop
我的while
循環有問題。
如果 while 循環條件為false(when the password is wrong)
,則它工作正常,但是當密碼正確時,它會同時寫入You are logged in
和Wrong password
。
我明白為什么會這樣,但我不明白如何解決這個問題。 我需要使用 while 循環,因為這是需要它的學校作業。
import java.util.Scanner;
public class Password {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String rightPassword = "Hello123";
System.out.println("Write your password: ");
String scan = scanner.nextLine();
while(scan.equals(rightPassword)){
System.out.println("You are logged in");
break;
}
System.out.println("Wrong password");
}
}
你應該做的是:
import java.util.Scanner;
public class Password {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String rightPassword = "Hello123";
String scan="";
while(true){
System.out.println("Write your password: ");
scan = scanner.nextLine();
if(scan.equals(rightPassword)) {
System.out.println("You are logged in");
break;
} else {
System.out.println("Wrong password");
}
}
}
}
在這個過程中,我們有一個無限循環,除非您提供正確的密碼,否則它不會中斷。 您也可以為此添加一些嘗試次數作為破壞條件。
根據I know, it become a infinite loop and that's why i use break. My requirement is that i must use a While loop. I know how to use a if-loop to solve the problem.
的評論I know, it become a infinite loop and that's why i use break. My requirement is that i must use a While loop. I know how to use a if-loop to solve the problem.
I know, it become a infinite loop and that's why i use break. My requirement is that i must use a While loop. I know how to use a if-loop to solve the problem.
您的解決方案應如下所示:
import java.util.Scanner;
public class Password {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String rightPassword = "Hello123";
System.out.println("Write your password: ");
while(!rightPassword.equals(scanner.nextLine())) {
System.out.println("Wrong password");
System.out.println("Write your password: ");
}
System.out.println("You are logged in");
}
}
您可以使用以下代碼。
int a=1;
Scanner scanner = new Scanner(System.in);
String rightPassword = "Hello123";
System.out.println("Write your password: ");
String scan = scanner.nextLine();
while(scan.equals(rightPassword)){
System.out.println("You are logged in");
a=0;
break;
}
if(a==1){
System.out.println("Wrong password");
}
without changing your code this will help you.
import java.util.Scanner;
public class Password {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String rightPassword = "Hello123";
System.out.println("Write your password: ");
String scan = scanner.nextLine();
while(scan.equals(rightPassword)){
System.out.println("You are logged in");
break end;
}
System.out.println("Wrong password");
end:
}
}
你可能有一些倒退的事情。 檢查錯誤的密碼,而不是循環中的正確密碼,然后再次詢問他們的密碼。 這樣,您將收到來自系統的一條消息,這是一個錯誤的密碼,或者在他們“成功登錄”后收到一條消息。
import java.util.Scanner;
public class Password {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String rightPassword = "Hello123";
System.out.println("Write your password: ");
String scan = scanner.nextLine();
while(!scan.equals(rightPassword)){
System.out.println("Wrong password. Please try again");
System.out.println("Write your password: ");
String scan = scanner.nextLine();
}
System.out.println("You are logged in");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.