Java 中断后程序无法继续运行;循环中
我已经为电梯创建了一个登录系统,该系统需要身份验证,目前运行良好。我遇到的问题是,程序在成功登录后不会继续运行。登录失败将在尝试3次失败后终止程序;这也很好。我相信这与我的Java 中断后程序无法继续运行;循环中,java,loops,Java,Loops,我已经为电梯创建了一个登录系统,该系统需要身份验证,目前运行良好。我遇到的问题是,程序在成功登录后不会继续运行。登录失败将在尝试3次失败后终止程序;这也很好。我相信这与我的中断有关行或我的括号位置。我试过使用继续相反,但这也不起作用。代码的下一部分在登录后不会运行,并且不会给出任何错误 这是我的密码 static Scanner console = new Scanner(System.in); public static void main(String[] args) { fin
中断有关代码>行或我的括号位置。我试过使用继续代码>相反,但这也不起作用。代码的下一部分在登录后不会运行,并且不会给出任何错误
这是我的密码
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.");
}
}
静态扫描仪控制台=新扫描仪(System.in);
公共静态void main(字符串[]args){
最终int UserID=5555;
最终整数密码=1234;
最终国际学生人数=22334455;
int EnteredUserID;
输入密码;
输入学生编号;
对于(int s=0;s 7 | | newFloor<0){
System.out.println(“无效楼层入口”);
}
否则{
int方向=0;
如果(当前楼层<新楼层){
方向=1;
}否则如果(当前楼层>新建楼层){
方向=-1;
}否则{
方向=0;
}
对于(;currentFloor!=newFloor;currentFloor+=newFloor)
系统输出打印项次(“…”+当前楼层);
System.out.println(“电梯到了!”);
}
}
公共火灾警报(){
System.out.println(“***火警***请安全离开大楼。”);
}
}
我可能错过了一些非常简单的东西,但似乎就是找不到。你所做的就是打破循环。这会将其发送出循环并发送到方法的其余部分,在本例中该部分为空。你没有指示它做任何事情。我想你的意思是:
if (UserID == EnteredUserID && (Password == EnteredPassword)
&& (StudentNumber == EnteredStudentNumber)) {
System.out.println("Athentication complete!");
System.out.println("***Elevator access granted!***");
System.out.println("Welcome...");
Elevator a = new Elevator(); //actually do something
Elevator.selectfloor();
break;
}
也就是说,假设电梯是一个类。三次失败后,for循环结束,因此方法返回,程序退出。成功登录后,您将跳出for循环。。。方法返回,程序退出
成功登录后,您希望发生什么事情。。。实现这一点的代码在哪里?在验证之后,您到底想去哪里?您当前正在打破循环,循环将其发送到下面的代码,但是。。。下面没有代码。循环的之后没有代码。打破循环。因此,它只会退出程序。在中断/成功登录后,我希望代码从private int currentlool开始运行代码>。