我已经创建了一个电梯的登录系统,要求身份验证,目前正常工作。我遇到的问题是程序在成功登录后不能继续运行。失败的登录将在3次失败尝试后终止程序;这也很好。我相信这与我的break;
系列或我的托架位置有关。我试过用continue;
来代替,但那也没用。代码的下一部分在登录后不会运行,也不会发生错误。中断后程序无法继续运行;在循环
这是我的代码;
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
final int UserID = 5555;
final int Password = 1234;
final int StudentNumber = 22334455;
int EnteredUserID;
int EnteredPassword;
int EnteredStudentNumber;
for (int s = 0; s <= 3; s++) {
if (s < 3) {
System.out.println("Enter your UserID to access lift;");
EnteredUserID = console.nextInt();
System.out.println("Your UserID is ==> " + EnteredUserID);
System.out.println("Enter your password to authenticate login;");
EnteredPassword = console.nextInt();
System.out.println("Password Entered is ==> " + EnteredPassword);
System.out.println("Enter your student number to finalise login and authentication;");
EnteredStudentNumber = console.nextInt();
System.out.println("Student Number Entered is ==> " + EnteredStudentNumber);
if (UserID == EnteredUserID && (Password == EnteredPassword)
&& (StudentNumber == EnteredStudentNumber)) {
System.out.println("Athentication complete!");
System.out.println("***Elevator access granted!***");
System.out.println("Welcome...");
break;
} else {
System.out.println("Wrong UserID, Password or Student Number. Please try again.");
}
} else {
System.out.println("3 incorrect enteries detected. Access Denied!");
}
}
}
private int currentFloor;
public Elevator() {
currentFloor = 0;
}
public void selectFloor() {
Scanner scnr = new Scanner(System.in);
int newFloor;
System.out.println("Enter your destination floor ==> ");
newFloor = scnr.nextInt();
if (newFloor > 7 || newFloor < 0) {
System.out.println("Invalid floor entry");
}
else {
int direction = 0;
if(currentFloor < newFloor){
direction = 1;
} else if (currentFloor > newFloor) {
direction = -1; ;
} else {
direction = 0;
}
for (; currentFloor != newFloor; currentFloor += newFloor)
System.out.println("..." + currentFloor);
System.out.println("Elevator has arrived!");
}
}
public void fireAlarm() {
System.out.println("***FIRE ALARM*** Please exit the building safely.");
}
}
我可能错过了一些非常简单的东西,但似乎无法找到它。
你想在认证之后去哪里?您目前正在摆脱循环,将其发送给下面的代码,但是......下面没有代码。 – Shahar
'for'循环之后没有代码。休息离开循环。所以它只能退出程序。 – AntonH
中断/成功登录后,我希望代码从'private int currentFloor;'开始运行。 – TomD