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方法是否正确地填充到数组中。推送方法来填充阵列
在调试器中逐步运行您的代码,您可以检查一切正在进行。 –
在'push()'中,您将所有数字设置为相同的数字。 – shmosel