繁体   English   中英

密码锁建议

[英]Combination Lock Advice

我正在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("F", "I", "U");

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

有人知道我在做什么错吗?

根据您显示给我们的代码,问题在于您的测试人员类没有尝试打开或关闭锁。 如果这不是真正的问题,请向我们展示所有测试人员课程。

我还要指出,您违反了标识符命名的编码标准 变量或字段名称应以小写字母开头。 Count更改为count并将MasterLock更改为masterLock

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM