2012-10-27 21 views
0

我在寫一個簡單的程序,用戶輸入密碼以使用目錄系統。檢查循環中的密碼

如果用戶選擇不輸入任何詳細信息,我想將它們註銷,然後給他們重新登錄的選項。我已經試過用循環做這個(見下面的代碼),但是當我將passQ可重置爲0時,我期待它將用戶登出並返回到循環中的開始語句......但是這沒有發生。

任何人都可以指向正確的方向。

我試着加入break; (註釋掉了)但是這只是結束了程序。

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

}

回答

2

你應該改變你的病情時

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

這將始終true

while (passQ != password) 

passQpassword不是指同一String實例。使用.equals()來比較兩個String實例的內容。但是,我看到無處在passQpassword設置爲不同的值,即使代碼:

while (!passQ.equals(password)) 

使用這仍然是true

0

使用while (!passQ.equals(password)) 總是使用equals()方法來比較字符串。