2014-03-31 67 views
0

\电梯此刻具有的功能,人们可以在电梯离开电梯,它扫描他们选择的地板,但是当下一个人选择他们不能即使他们可以选择他们想要的7个楼层中的任何一个,但从0到0的任何地方去。如何使电梯扫描编号

import java.io.IOException; 
import java.util.Scanner; 

class LiftLogin { 

    private static int CurrentFloor; 
    static int RequestedFloor; 
    private static Scanner next2; 
    private static int i; 

    public static <infinite> void main(final String args[]) throws IOException, InterruptedException { 
     { 
      Scanner authentification = new Scanner(System.in); 
      String Passname; 
      System.out.println("Please enter your Encrypted Passname"); 
      Passname = authentification.nextLine(); 

      System.out.println("Doors Opening"); 
      Thread.sleep(5000); 
      int attempts = 0; 

      if (Passname.equals("uzp mwzrrd")) { 
       System.out.println("Access Granted For Joe Bloggs"); 
      } else if (Passname.equals("ncltr dxtes")) { 
       System.out.println("Access Granted For Craig Smith"); 
      } else if (Passname.equals("nsctd zyptw")) { 
       System.out.println("Access Granted For Chris ONeil"); 
      } else if (Passname.equals("pxxl dezyp")) { 
       System.out.println("Access Granted For Emma Stone"); 
      } else { 
       System.out.println("Incorrect Passname"); 
      } 
      { 
       attempts++; 
       if (attempts >= 3) { 
        System.out.println("\nYou've had 3 Attempts, you have been denied Access"); 
       } 
      } 
     } 

     CurrentFloor = i; 

     next2 = new Scanner(System.in); 
     int NewFloor; 
     System.out.println("Current Level:" + CurrentFloor); 

     infinite loop; 
     for (;;) { 

      System.out.println("Please Choose a Floor:"); 
      NewFloor = next2.nextInt(); 
      if ((NewFloor > 7) || (NewFloor < 0) || (NewFloor == 7)) { 
       System.out.println("\nWrong Floor Selected"); 
      } 

      else if ((NewFloor <= 7) && (NewFloor > 0) && (NewFloor != 7)) { 
       for (int i = 0; i <= NewFloor; i++) { 
        System.out.println("Floor Level: " + i); 
        Thread.sleep(1000); 
       } 
       { 
        System.out.println("Your at Your Destination - Floor:" + NewFloor); 
        Thread.sleep(5000); 
        System.out.println("Doors Closing"); 
        Thread.sleep(1000); 
        System.out.println("Floor Level: " + NewFloor); 
        Thread.sleep(1000); 
        // The code only reprints from 0 i want it to print from the 
        // NewFloor 
       } 
      } 
     } 
    } 
} 
+1

你是什么实际问题? – cheseaux

+0

我怎样才能从用户出去的楼层扫描电梯而不是从0开始扫描? – user3480980

+0

您应该以某种方式存储电梯到达的最后一层,并在此编号处启动'for'循环。 – cheseaux

回答

0

你应该初始化你的我:private static int i = 0;

我想你错过了什么是你永远不会改变的价值你CurrentFloor

您应该更改您的代码:

infinite loop; 
    for (;;) { 

     System.out.println("Please Choose a Floor:"); 
     NewFloor = next2.nextInt(); 
     if ((NewFloor >= 7) || (NewFloor < 0)) { 
      System.out.println("\nWrong Floor Selected"); 
     } 
     else if (CurrentFloor == NewFloor) 
     { 
      System.out.println("You are already at this floor"); 
      continue; 
     } 
     else if ((NewFloor < 7) && (NewFloor > 0)) 
     { 
      if(CurrentFloor < NewFloor) 
      { 
       for (int i = CurrentFloor; i <= NewFloor; i++) { 
       System.out.println("Floor Level: " + i); 
       Thread.sleep(1000); 
       } 
      } 
      else 
      { 
       for (int i = CurrentFloor; i >= NewFloor; i--) { 
       System.out.println("Floor Level: " + i); 
       Thread.sleep(1000); 
       } 
      } 
      { 
       System.out.println("Your at Your Destination - Floor:" + NewFloor); 
       Thread.sleep(5000); 
       System.out.println("Doors Closing"); 
       Thread.sleep(1000); 
       System.out.println("Floor Level: " + NewFloor); 
       Thread.sleep(1000); 
       // The code only reprints from 0 i want it to print from the 
       // NewFloor 
       CurrentFloor = NewFloor; // change the value of your currentFloor 
      } 
     } 
    } 

我让你看看你想要添加什么。

+0

感谢您的反馈,这真的很有帮助,并允许代码做我想做的事,其余的我应该能够自己做,谢谢大家Clad Clad和其他所有的建议 – user3480980

+0

没有问题;)别忘了验证答案是否有效;) –

0

的第一件事是:

((NewFloor > 6) || (NewFloor < 0)) 

将做的工作,而不是:

其次,你可以这样开始:

for (int i = CurrentFloor; i <= NewFloor; i=(CurrentFloor>NewFloor)? i+1:i-1) { 
       if (CurrentFloor==NewFloor) { System.out.println("You are already on:); } 
       System.out.println("Floor Level: " + i); 
       Thread.sleep(1000); 
      } 
//and add this statement which will keep the track of the last user get out floor 
CurrentFloor=NewFloor;