2013-10-20 50 views
2

我在Netbeans中創建了一個組合鎖類,我很困惑爲什麼當我運行該文件時我沒有收到任何輸出。任何人都知道我在做什麼錯了?任何和所有的幫助將不勝感激!這裏是我的建築工類代碼:密碼鎖援助

package combinationlock ; 
/** 
* A class to implement a combination lock with 26 dial positions 
* and a three-letter combination 
* 
* @Carlos 
*/ 
public class CombinationLock 
{ 
// instance variable declarations go here 
private boolean open ; 
private int Count ; 
private String position1 ; 
private String position2 ; 
private String position3 ; 
private String first = "F" ; 
private String second = "I" ; 
private String third = "U" ; 


/** 
* Creates a lock with a given combination consisting of three upper-case characters. 
* @param first the first letter of the combination 
* @param second the second letter of the combination 
* @param third the third letter of the combination 
*/ 
public CombinationLock(String first, String second, String third) 
{ 
    this.first = first ; 
    this.second = second ; 
    this.third = third ; 
    open = false ; 
    Count = 0 ; 
} 


/** 
* Set the dial to a position 
* @param aPosition a String consisting of a single uppercase letter (A..Z) 
*/ 
public void setPosition(String aPosition) 
{ 
    if (Count == 0)   
    { 
     position1 = aPosition ; 
     Count = Count + 1 ; 
    } 
    else if (Count == 1) 
    { 
     position2 = aPosition ; 
     Count = Count + 1 ; 
    } 
    else if (Count == 2) 
    { 
     position3 = aPosition ; 
    } 
} 

/** 
    * Try opening the lock 
*/ 
public void tryToOpen() 
{ 
if (first.equals(position1) && second.equals(position2) && third.equals(position3)) 
{ 
    open = true ; 
    System.out.println("Its open!") ; 
} 
else 
{ 
    open = false ; 
    System.out.println("Wrong combination! Please try again.") ; 
} 
} 

/** 
    * Check whether the lock is open 
    * @return true or false indicating whether the lock is open 
*/ 
public boolean isOpen() 
{ 
    return open ; 
} 

/** 
    * Close the lock and print a message indicating that the lock is now closed 
*/ 
public void lock() 
{ 
    Count = 0 ; 
    open = false ; 
    System.out.println("You re-apply the lock") ; 
} 
} 

這裏是我在我的測試類中使用的代碼:

package combinationlock ; 

import javax.swing.JOptionPane ; 
/** 
* 
* @author Carlos 
*/ 
public class CombinationLockTester 
{ 
    public static void main (String[] args) 
{ 
    CombinationLock MasterLock = new CombinationLock("A", "B", "C"); 

    String input = JOptionPane.showInputDialog 
      ("Please enter first letter.") ; 

    MasterLock.setPosition(input) ; 

    String input2 = JOptionPane.showInputDialog 
      ("Please enter second letter.") ; 

    MasterLock.setPosition(input2) ; 

    String input3 = JOptionPane.showInputDialog 
      ("Please enter third letter") ; 

    MasterLock.setPosition(input3); 

    System.out.println("The combination entered was " +input + input2 +input3) ; 

} 
} 
+0

您的輸入是什麼? –

+0

您是否在控制檯上看到任何輸出?即使從計劃提示輸入? –

+0

它只是顯示「輸入的組合是...」,然後沒有別的我真的不知道我做錯了什麼。我輸入了A,B,C,然後我做了A,A,A,看看有沒有什麼會改變,但仍然沒有... – Saliva

回答

1

您在MasterLock設置位置,但不調用tryToOpen方法。試試這個,看看你得到任何輸出:

MasterLock.tryToOpen(); 

原本應該在三個調用後調用。