2014-03-24 65 views
-1

我已经创建了一个电梯的登录系统,要求身份验证,目前正常工作。我遇到的问题是程序在成功登录后不能继续运行。失败的登录将在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."); 


} 

} 

我可能错过了一些非常简单的东西,但似乎无法找到它。

+0

你想在认证之后去哪里?您目前正在摆脱循环,将其发送给下面的代码,但是......下面没有代码。 – Shahar

+5

'for'循环之后没有代码。休息离开循环。所以它只能退出程序。 – AntonH

+0

中断/成功登录后,我希望代码从'private int currentFloor;'开始运行。 – TomD

回答

2

你正在做的是你breaking没有循环。这将它从循环中发送出来,并发送到方法的其余部分,在这种情况下它是空的。你并没有指示它做任何事情。我认为你的意思是:

 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; 
     } 

也就是说,假设Elevator是一类。

0

三次失败后,for循环结束,所以方法返回并退出程序。成功登录后,您跳出for循环...方法返回并退出程序。

您预期在成功登录时发生了什么......以及在何处发生这种情况的代码?