从特定点循环到Java中的另一个点?

时间:2015-12-23 10:04:57

标签: java loops while-loop

我正在开发一个类似于数字时间机器的Java项目,它基本上可以保存特定状态的内容,将其存档到特定文件,并将其存储在某个目录中,以便安全保存。现在我正在研究密码功能。我有基础知识来检查它是对还是错,但当我输入错误答案时,课程在说“无效密码”后结束。 有没有办法让它从else语句的末尾循环到main的开头,所以我不必每次都重新执行该类?

import java.util.Scanner;
import java.util.*;

public class Adam {

    public static void main(String args[]) {

        String passwrd = "password";
        String inputPassword;

        System.out.print("Please Input Password ");
        Scanner sc = new Scanner(System.in);
        inputPassword = sc.nextLine();

        if (inputPassword.equals(passwrd)) {
            System.out.println("Password Accepted, Loading Archive...");
        } else {
            System.out.println("Invalid Password");
        }
    }
}

8 个答案:

答案 0 :(得分:3)

你可以使用do while循环,如果用户输入无效,则不要继续循环。

import java.util.Scanner;
import java.util.*;

public class Adam {

    public static void main(String args[]) {
        String passwrd = "password";

        String inputPassword;
        boolean b = true;
        Scanner sc = new Scanner(System.in);
        do {

            System.out.print("Please Input Password ");

            inputPassword = sc.nextLine();

            if (inputPassword.equals(passwrd)) {

                System.out.println("Password Accepted, Loading Archive...");
                b = false;
            } else {

                System.out.println("Invalid Password");
                b = true;

            }

        } while (b);

    }
}

DEMO

答案 1 :(得分:2)

每当您想运行某些代码时,请检查结果,然后在必要时重复最明显的结构do while

如下所示:

String inputPassword = null;
do {
    if (inputPassword != null)
        System.out.println("Invalid password");
     System.out.println("Enter password");
     inputPassword = sc.nextLine();
} while (!inputPassword.equals(password));

答案 2 :(得分:0)

只需将整个代码放入while(true)循环中,如果密码为walid,则会中断

public static void main(String args[]) 
{
    String passwrd = "password";
    String inputPassword;

    while(true)
    {    
        System.out.print("Please Input Password ");
        Scanner sc = new Scanner(System.in);
        inputPassword = sc.nextLine();

        if (inputPassword.equals(passwrd)) 
        {
            System.out.println("Password Accepted, Loading Archive...");
            break; //here you are getting out of loop
        } 
        else 
        {
            System.out.println("Invalid Password");
            //you are not breaking loop so the program will return to the beggining
        }
    }
}

答案 3 :(得分:0)

    public static void main(String args[]) {
       String password = "password";
       String inputPassword;

        while(inputPassword == null || !inputPassword.equals(password)){
           System.out.print("Please Input Password ");
           Scanner sc = new Scanner(System.in);
           inputPassword = sc.nextLine();

           if (!inputPassword.equals(password)) {
               System.out.println("Invalid Password");
           } 
       }
       System.out.println("Password Accepted, Loading Archive...");
    }

答案 4 :(得分:0)

尝试输入密码的次数有限,这是一个好主意。例如,要尝试输入三次密码,您将拥有以下代码:

Scanner sc = new Scanner(System.in);
for (i = 0; i < 4; ++i) {
    System.out.print("Please Input Password ");
    inputPassword = sc.nextLine();

    if (inputPassword.equals(passwrd)) {
        System.out.println("Password Accepted, Loading Archive...");
        break;
    } 
    else {
        System.out.println("Invalid Password");
    }
}

答案 5 :(得分:0)

我在程序中添加了一个while循环和一个标志。请看一下:

public static void main(String[] args) {
        String passwrd = "password";

        boolean wrongPassword = true;

        while(wrongPassword){

            String inputPassword;

            System.out.print("Please Input Password ");
            Scanner sc = new Scanner(System.in);
            inputPassword = sc.nextLine();

            if (inputPassword.equals(passwrd)) {

                System.out.println("Password Accepted, Loading Archive...");
                wrongPassword = false;

            } 
            else {
                System.out.println("Invalid Password");
            }
        }

    }

答案 6 :(得分:0)

使用do-while循环:

String passwrd = "password";
String inputPassword ="";
Scanner sc = new Scanner(System.in);

 do{
    System.out.print("Please Input Password ");
   // get the password from user
    inputPassword = sc.nextLine();

    // if password matches print success and break out of loop
    if (inputPassword.equals(passwrd)) {
       System.out.println("Password Accepted, Loading Archive...");
       break;
    } 
    else{
       System.out.println("Invalid Password");
     }

   }while(true);

答案 7 :(得分:0)

使用do-while循环。此外,您的代码存在一些安全问题:

  1. 不要将System.in用于密码。请改用Console。 (但是,如果从IDE运行程序,则可能没有控制台。在这种情况下,请改用GUI方法。)

  2. 输入密码后直接隐藏密码。

  3. 输入后尽快清除密码缓冲区(即覆盖密码缓冲区)。请参阅Console文档。

    Console con = System.console();
    if (con == null)
        throw new IOException("This JVM has no console");
    
    boolean pwdCorrect;
    do {
        char[] inputPassword = con.readPassword("Please input password: ");
        String hashedInputPassword = hashPassword(inputPassword);  // search Internet for how to hash a password
        for (int i = 0; i < inputPassword.length; i++)
            inputPassword[i] = '\0';
        pwdCorrect = isValidPassword(hashedInputPassword);  // e.g. by looking it up in a password file
        if (pwdCorrect)
            System.out.println("Password accepted. Loading archive...");
        else
            System.out.println("Invalid password.");
    } while (!pwdCorrect);