2016-01-29 60 views
1
import java.util.*; 

public class Lock { 
    private int combination = 1729; 
    private int input; 
    int[] code = new int[4]; 

    public void push(int button){ 
     for(int i = 0; i < 4; i++){ 
      code[i] = button; 
     } 
    } 
    public boolean open(){ 
     boolean results = false; 
     int boop = 0; 
     for (int i = 0;i < 4; i++){ 
      boop = boop*10 + code[i]; 
     } 
     if(boop == combination){ 
      results = true; 
     } 
     return results; 
    } 
} 
And here is the tester 

public class LockTester 
{ 
    public static void main(String[] args) 
    { 
     Lock myLock = new Lock(); 
     myLock.push(1); 
     myLock.push(7); 
     myLock.push(3); 
     myLock.push(9); 
     System.out.println(myLock.open()); 
     System.out.println("Expected: false"); 
     myLock.push(1); 
     myLock.push(7); 
     myLock.push(2); 
     myLock.push(9); 
     System.out.println(myLock.open()); 
     System.out.println("Expected: true"); 
     myLock.push(1); 
     myLock.push(7); 
     myLock.push(2); 
     System.out.println(myLock.open()); 
     System.out.println("Expected: false"); 
     myLock.push(9); 
     System.out.println(myLock.open()); 
     System.out.println("Expected: false"); 
     myLock.push(1); 
     myLock.push(7); 
     myLock.push(2); 
     myLock.push(9); 
     System.out.println(myLock.open()); 
     System.out.println("Expected: true"); 
    } 
} 

我每次都会出现错误。我不确定push方法是否正确地填充到数组中。推送方法来填充阵列

+0

在调试器中逐步运行您的代码,您可以检查一切正在进行。 –

+1

在'push()'中,您将所有数字设置为相同的数字。 – shmosel

回答

1

在您当前的方法中,您将每个按钮按下时将所有4个按钮分配给相同的输入。为了解决这个问题,你需要保持一些内部状态来表示哪些按键已经被锁定。在我的方法来此,用户可以按4个组合键,并试图打开该锁将键盘恢复到原来的状态:

public class Lock { 
    private int combination = 1729; 
    private static int CODE_LENGTH = 4; 
    private int input = 0;  // this will keep track of which button to press 
    int[] code = new int[CODE_LENGTH]; 

    public void push(int button){ 
     if (input >= CODE_LENGTH) { 
      System.out.println("All keys have been entered. Please try to open the lock."); 
      return; 
     } 

     // assign a single button press here 
     code[input] = button; 
     ++input; 
    } 

    public boolean open() { 
     input = 0; // reset the keypad to its initial state here 
     int boop = 0; 
     for (int i=0; i < CODE_LENGTH; i++) { 
      boop = boop*10 + code[i]; 
     } 
     if (boop == combination) { 
      return true; 
     } 

     return false; 
    } 
} 
+0

您需要重置输入值,否则它将抛出ArrayIndexOutOfboundException – thedarkpassenger

+0

@AnshulJain I _do_将输入值重置为零。仔细查看我的代码。 –

0
public void push(int button){ 
    for(int i = 0; i < 4; i++){ 
     code[i] = button; 
    } 
} 

这部分代码看起来是错误的。当你正在推动使用myLock.push(n)阵列中的每个元素,数组获得由n个所有indexes.Looking你的代码填充它看起来像后

myLock.push(1); 
    myLock.push(7); 
    myLock.push(3); 
    myLock.push(9); 

你期待数组为[1,7,3,9 ]。但实际上它会是[9,9,9,9]。

你可以试试这个。

public class LockTester 
{ 
    public static void main(String[] args) 
    { 
     Lock myLock = new Lock(); 
     int[] c={1,7,2,9}; 
     myLock.push(c); 
     System.out.println(myLock.open()); 
     System.out.println("Expected: false"); 
    } 
} 


import java.util.*; 
public class Lock { 
private int combination = 1729; 
private int input; 
int[] code = new int[4]; 

public void push(int[] button){ 
    for (int i=0;i<button.length;i++){ 
     code[i]=button[i]; 
    } 
} 
public boolean open(){ 
    boolean results = false; 
    int boop = 0; 
    for (int i = 0;i < 4; i++){ 
     System.out.println(code[i]); 
    } 
    if(boop == combination){ 
     results = true; 
    } 
    return results; 
    } 
}