Я действительно новичок в Java, и у меня возникают проблемы с программой, необходимой для моего класса Java.Мне нужно смоделировать блокировку и реализовать методы, которые меняют комбинацию, проверяют число сверху и т. Д.
Ну, я думаю, что есть проблема с моим методом openLock()
или alterLockCombination()
.Моя программа правильно откроет блокировку с комбинацией по умолчанию (0,0,0), но когда я попытаюсь изменить комбинацию, она не будет работать должным образом, и только комбинация по умолчанию разблокирует блокировку.
Где моиздесь ошибки?
import java.util.Scanner;
public class Lock {
public static final int CLOCKWISE = 0;
public static final int COUNTER_CLOCKWISE = 1;
Scanner in = new Scanner(System.in);
private int x;
private int y;
private int z;
private boolean isLockOpen;
private int noOnTopOfKnob;
public Lock() {
x = 0;
y = 0;
z = 0;
this.isLockOpen = false;
this.noOnTopOfKnob = 0;
}
public void alterLockCombinaiton(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public void turnKnob(int direction, int noToStop){
int i = noOnTopOfKnob;
int numbersPassed = 0;
System.out.println("Simulating......");
do{
if(direction == CLOCKWISE)
i++;
else if(direction == COUNTER_CLOCKWISE)
i--;
if(i > 39)
i = 0;
if (i < 0)
i=39;
this.noOnTopOfKnob = i;
System.out.print(noOnTopOfKnob + " ");
numbersPassed++;
if(numbersPassed>40 && noOnTopOfKnob==noToStop)
break;
}
while(true);
System.out.println();
}
public void closeLock() {
System.out.println("Locked!");
this.isLockOpen = false;
}
public boolean openLock() {
// initializing with arbitrary values
int firstStop = -1;
int secondStop = -1;
int thirdStop = -1;
int firstRotation = -1;
int secondRotation = -1;
int thirdRotation = -1;
for(int i = 1; i <= 3; i++){
System.out.print("Enter a number (0-39) " + i + ": ");
int noToStop = in.nextInt();
System.out.print("Enter 0 for clockwise and 1 for counter-clockwise) " + i + ": ");
int direction = in.nextInt();
turnKnob(direction, noToStop);
if(i == 1) {
firstStop = noToStop;
firstRotation = direction;
}
else if(i == 2) {
secondStop = noToStop;
secondRotation = direction;
}
else if(i == 3) {
thirdStop = noToStop;
thirdRotation = direction;
}
if(firstStop == this.x && firstRotation == CLOCKWISE
&& secondStop == this.y && secondRotation == COUNTER_CLOCKWISE
&& thirdStop == this.z && thirdRotation == CLOCKWISE) {
this.isLockOpen = true;
}
}
return isLockOpen;
}
public boolean isLockOpen() {
return this.isLockOpen;
}
public int getNoAtTop() {
return noOnTopOfKnob;
}
}
КОНЕЦ Lock.java
import java.util.Scanner;
public class LockInput {
public static void main(String[] args) {
System.out.println("\nWelcome to lock simulator");
System.out.println("-------------------------------------------------");
menu();
}
public static void menu() {
Scanner scnr = new Scanner(System.in);
Lock newLock = new Lock();
int xInput, yInput, zInput;
System.out.println("\nSelect an option for the lock.\n");
System.out.println(" A : Set a new lock combination. ");
System.out.println(" B : Close the lock.");
System.out.println(" C : Attempt to open the lock.");
System.out.println(" D : Check lock status.");
System.out.println(" E : Check current top number.");
System.out.println(" Q : Quit program.");
char menuOption = scnr.next().charAt(0);
menuOption = Character.toUpperCase(menuOption);
switch(menuOption) {
case 'A':
System.out.println("Set a new combination for the lock.\n");
System.out.println("Enter the first number of the combination.");
xInput = scnr.nextInt();
System.out.println("Enter the second number of the combination.");
yInput = scnr.nextInt();
System.out.println("Enter the third number of the combination.");
zInput = scnr.nextInt();
newLock.alterLockCombinaiton(xInput,yInput,zInput);
menu();
break;
case 'B':
newLock.closeLock();
menu();
break;
case 'C':
newLock.openLock();
System.out.println("-------------------------------------------------");
System.out.println("After lock open attemp....");
System.out.println("No on top: " + newLock.getNoAtTop());
System.out.println("Lock is open: " + newLock.isLockOpen());
menu();
break;
case 'D':
System.out.println("Lock is open: " + newLock.isLockOpen());
menu();
break;
}
}
}
Класс LockInput не завершен, так как я сначала пытаюсь выяснить свою проблему ввода.