java - 密码锁辅助

标签 java locking combinations

我正在 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) ;

}
}

最佳答案

您正在 MasterLock 上设置位置,但没有调用 tryToOpen 方法。试试这个,看看你是否得到任何输出:

MasterLock.tryToOpen();

应该在对 setPosition 的三个调用之后调用。

关于java - 密码锁辅助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19472759/

相关文章:

java - 从 JSON 读取字段到 POJO 时获取空指针

php - 当不应该通过 PHP MySQL/MySQLi 有两个(应该是)单独的连接时,GET_LOCK 会成功

java - 查找包含可变数量元素的可变数量数组的排列的有效方法

java - 推理引擎的java实现

java - Prefuse 卡住 JFrame

java - 获取锁失败的线程会发生什么?

java - java多个setText之间的延迟

synchronization - 无硬件支持的锁定

locking - log4net RollingFileAppender 间歇性锁定文件问题

javascript - 返回和小于或等于 n 的数组中所有可能的数字组合