[英]Java looping to an original statement
我是Java新手,需要一些語法問題的幫助。 基本上我有一個提示,要求用戶輸入密碼。 如果他們輸入密碼,則會顯示輸入的字符串密碼並保存密碼。 我需要幫助的是,如果用戶不輸入任何字符串而只是按下Enter鍵,我希望出現原始提示。 我希望它一直循環直到輸入密碼。 下面是我的原始代碼,以及我嘗試過的for循環。 我需要協助。 謝謝
String prompt = "Enter password ";
String pw = passwordProvider.getPassword(prompt);
if (pw != null && pw.length() > 0)
{
passwordProvider.status("Password Updated");
} else {
System.out.println("here is where I want my original prompt to appear again");
}
return DRPwCrypt.encryptAES(pw);
帶for循環的代碼
String prompt = "Enter password ";
String pw = passwordProvider.getPassword(prompt);
if (pw != null && pw.length() > 0)
{
passwordProvider.status("Password Updated");
} else {
System.out.println("here is where I want my original prompt to appear again");
}
CODE WITH FOR LOOP
for (
String prompt = "Entry " + propertyName + " found in config " + configName +
"\nEnter new password : ";) {
String pw = passwordProvider.getPassword(prompt);
if (pw != null && pw.length() > 0)
{
passwordProvider.status("Updating password");
} else {
System.out.println("etsting");
}
return DRPwCrypt.encryptAES(pw);
}
當您要迭代預定義的次數時, 通常使用for
循環。
在您的情況下, while
循環將是一個更好的選擇:
boolean correctPassword = false;
while(!correctPassword) {
String prompt = "Enter password ";
String pw = passwordProvider.getPassword(prompt);
if (pw != null && pw.length() > 0)
{
passwordProvider.status("Password Updated");
correctPassword = true;
} else {
System.out.println("Invalid password, please try again");
}
}
當您要繼續迭代直到給定條件為true
或false
(視情況而定)時,將使用while
循環。
上面的代碼也可以修改為使用for
循環:
boolean correctPassword = false;
for(;!correctPassword;) {
String prompt = "Enter password ";
String pw = passwordProvider.getPassword(prompt);
if (pw != null && pw.length() > 0)
{
passwordProvider.status("Password Updated");
correctPassword = true;
} else {
System.out.println("Invalid password, please try again");
}
}
上面的代碼應該創建一個無限的for
循環,該循環將一直迭代直到標志為true
為止。 如您所見,這可能比while
循環更不易讀和/或更混亂。
編輯:此代碼為我有效:
import java.util.*;
public class HelloWorld{
public static void main(String []args){
boolean correctPassword = false;
while(!correctPassword) {
Scanner input = new Scanner(System.in);
String prompt = "Enter password ";
String pw = input.nextLine();
System.out.println(pw.length());
if (pw != null && pw.length() > 0)
{
System.out.println("Password Updated");
correctPassword = true;
} else {
System.out.println("Invalid password, please try again");
}
}
}
}
您也可以嘗試使用Java的Console類
嘗試這個:
public static void main(String[] args) {
Console console = System.console();
if(console == null){
System.out.println("ERROR: Couldn't get Console");
return;
}
boolean passwordUpdated = false;
while (!passwordUpdated) {
System.out.print("Enter password :");
String password = console.readLine();
// char[] password = console.readPassword() // can be used
if (password != null && password.length() > 0) {
System.out.println("Password Updated");
passwordUpdated = true;
} else {
System.out.println("WARN: Invalid password, please try again");
}
}
}
注意:您必須使用終端/命令提示符來運行程序,在eclipse上系統returns
空控制台對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.