简体   繁体   English

Java循环到原始语句

[英]Java looping to an original statement

I am new to java and need some help with some syntax issues. 我是Java新手,需要一些语法问题的帮助。 Basically what I have is a prompt which asks users to enter a password. 基本上我有一个提示,要求用户输入密码。 If they enter a password, then the string password entered is displayed and the password is saved. 如果他们输入密码,则会显示输入的字符串密码并保存密码。 What I need help with is if the user does not enter any string but just hits the enter button, I want the original prompt to appear. 我需要帮助的是,如果用户不输入任何字符串而只是按下Enter键,我希望出现原始提示。 I want it to keep looping till a password is entered. 我希望它一直循环直到输入密码。 Below is my original code, and what I tried with a for loop. 下面是我的原始代码,以及我尝试过的for循环。 I need some help. 我需要协助。 Thank you 谢谢

    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);

Code with for loop 带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);

} }

The for loop is usually used when you want to iterate a predefined number of times. 当您要迭代预定义的次数时, 通常使用for循环。

In your case, a while loop would be a better option: 在您的情况下, 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");
     }
}

while loops are used when you want to keep on iterating until a given condition is true or false (as the case may be). 当您要继续迭代直到给定条件truefalse (视情况而定)时,将使用while循环。

The code above could also be modified to use a for loop instead: 上面的代码也可以修改为使用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");
     }
}

The above should create an infinite for loop which will keep on iterating until the flag is true . 上面的代码应该创建一个无限的for循环,该循环将一直迭代直到标志为true为止。 As you can see, this might be less readable and/or more confusing than the while loop. 如您所见,这可能比while循环更不易读和/或更混乱。

EDIT: This code works as epxected for me: 编辑:此代码为我有效:

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");
             }
        }
     }
}

You can also try using Java's Console class 您也可以尝试使用Java的Console

Try this: 尝试这个:

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");
        }
    }
}

Note: you have to use terminal/command-prompt to run the program, on eclipse System returns null console object. 注意:您必须使用终端/命令提示符来运行程序,在eclipse上系统returns空控制台对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM