0

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

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

谁能指出我正确的方向。

我尝试添加中断;(在下面注释掉)但这刚刚结束了程序。

ps 我知道这种管理密码的方式并不安全,我只是在玩游戏,因为我将来可能想将这样的循环用于其他事情。

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

}

4

3 回答 3

3

这将永远true

while (passQ != password)

aspassQpassword不引用同一个String实例。用于.equals()比较两个String实例的内容。但是,我在代码中看不到passQ或被password设置为不同值的任何地方,即使:

while (!passQ.equals(password))

被使用这仍然是true

于 2012-10-27T11:49:30.270 回答
2

你应该改变你的while条件

   while (!passQ.equals(password))

或者用下面的代码替换它,请在while循环上方发布代码。

  if (passCheck.hasNextInt()) { //check whether user has entered numeric value
        if (passCheck.nextInt() == 0) { //if 0 close the program 
            return;
        }
    }
于 2012-10-27T11:49:03.763 回答
0

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

于 2012-10-27T11:51:01.110 回答