2014-03-31 24 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;