在循环中检查密码

时间:2012-10-27 11:47:30

标签: java while-loop

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

如果用户选择不输入任何我要将其注销的详细信息,然后再给他们选择重新登录。我已经尝试用循环执行此操作(请参阅下面的代码),但是当我将passQ vairable重置为0时,我期望它将用户注销并返回循环中的开始语句...但这并不会发生。

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

我尝试添加休息; (评论说)但这刚刚结束了该计划。

P.S。我知道这种管理密码的方式并不是很安全,我只是玩游戏,因为我可能希望将来使用这样的循环。

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

}

3 个答案:

答案 0 :(得分:3)

这将始终true

while (passQ != password)

由于passQpassword未引用相同的String实例。使用.equals()比较两个String实例的内容。但是,我在代码中看不到passQpassword被设置为不同的值,所以即使:

while (!passQ.equals(password))
使用

仍然是true

答案 1 :(得分:2)

你应该改变你的状态

   while (!passQ.equals(password))

或者用以下内容替换它,请在循环播放上面的代码。

  if (passCheck.hasNextInt()) { //check whether user has entered numeric value
        if (passCheck.nextInt() == 0) { //if 0 close the program 
            return;
        }
    }

答案 2 :(得分:0)

使用while (!passQ.equals(password)) 始终使用equals()方法来比较字符串。