java - 循环检查密码

标签 java while-loop

我正在编写一个简单的程序,用户输入密码来使用目录系统。

如果用户选择不输入任何详细信息,我想将他们注销,然后给他们重新登录的选项。我尝试用循环执行此操作(请参见下面的代码),但是当我将 passQ vairable 重置为 0 时,我希望它能够将用户注销并返回到循环中的开头语句...但这并没有发生。

任何人都可以指出我正确的方向吗?

我尝试添加中断; (下面评论)但这刚刚结束了程序。

附:我知道这种管理密码的方式并不真正安全,我只是玩玩,因为我将来可能想在其他事情上使用这样的循环。

package directory;

import java.util.ArrayList;
import java.util.Scanner;

public class Directory {

/**
 * @param args the command line arguments
 */
//-------------//
//Main Meathod //
//-------------//
public static void main (String[] args)
{
    //------------------//
    // Define Variables //
    //------------------//
    String question = "Y";
    String passQ    = "0";
    String password = "password";

    Scanner passCheck = new Scanner(System.in);
    System.out.println("Welcome to Chris Headleands Tel Directory, Enter Password to access sytems"); //1 and 0 used for yes and no
    question = passCheck.nextLine(); // Next line used to decide if the user wants to enter a persons details
    while (passQ != password)
    {
        if (question.equalsIgnoreCase("password")) //Do they want to ender a person? 1 == yes
        {

            //Create a new array list for the people created
            ArrayList<TelEntry> directory = new ArrayList<TelEntry>();

            //Check if the user wants to create a new person
            Scanner checker = new Scanner(System.in);
            System.out.println("would you like to add a directory entry(1 = Yes / 0 = NO!)"); //1 and 0 used for yes and no
            question = checker.nextLine(); // Next line used to decide if the user wants to enter a persons details

            if (question.equalsIgnoreCase("y")) //Do they want to ender a person? 1 == yes
            {
                while (question.equalsIgnoreCase("y")) // Add a loop so multiple people can be added
                {

                    String name; // First name
                    String telNo; // Last name

                    // Create scanner for entering details
                    Scanner keybd = new Scanner(System.in);

                    //----------------------------------//
                    //User to enter details via keyboard//
                    //----------------------------------//

                    // Ask user to set first name
                    System.out.println("Enter their first name");
                    name = keybd.nextLine();

                    // Ask user to set last name
                    System.out.println("Enter their last name");
                    telNo = keybd.nextLine();

                    // -------------------------------------//
                    //Add the new person to personlist array//
                    //--------------------------------------//
                    directory.add(new TelEntry(name, telNo));

                    //-----------------------------------------------------------------------------//
                    // Check to see if the user wants to add another person, if Y then re-run loop //
                    //-----------------------------------------------------------------------------//
                    Scanner newChecker = new Scanner(System.in);
                    System.out.print("would you like to enter another person? (1 = Yes / 0 = NO!)");
                    question = newChecker.nextLine();  

                }


            }

            //--------------------------------------------------------------//
            // User doesnt want to add any people to the persontest program //
            //--------------------------------------------------------------//
            if (question.equalsIgnoreCase("testmode"))
            {
                System.out.println("you are now in test mode")
            }
            else
            { // Provide the user with a witty retort                     
                System.out.println("if your not going to use me for what I was designed to do, bugger off and bother someone else!");
                System.out.println("Logging out");
                passQ = "0"; // Reset password loop
                // break;
            }
        }
    }
}

}

最佳答案

这将永远true:

while (passQ != password)

as passQpassword 不引用相同的 String 实例。使用 .equals() 比较两个 String 实例的内容。但是,我在代码中没有看到 passQpassword 设置为不同的值,因此即使:

while (!passQ.equals(password))

如果使用的话,这仍然是true

关于java - 循环检查密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099891/

相关文章:

java - 执行满足 2 个条件之一的 while 循环

java - HttpURLConnection#getResponseCode() 导致 FileNotFoundException

java - 如何在不中断java程序其余部分的情况下中断while循环?

c - 为什么输出是 36 而不是 24?

JavaFX 0x300b : Could not get EGL surface

c++ - 如果输入错误则无限循环

php - 如何使用逗号组合数组中的所有元素?

java - 在java中将整数添加到JList

java - 如何修复 Fortify 路径操纵(输入验证和表示、数据流)漏洞

java - 如何使用 Joda 时间库计算下一个生日