如何进行电梯扫描数字

时间:2014-03-31 12:11:28

标签: java println

\此时电梯具有人们可以在电梯中获得的功能离开电梯并且它扫描他们选择的楼层但是当下一个人选择楼层时他们无法从任何地方去,但是0即使他们可以选择他们想要的7个楼层中的任何一个。

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

2 个答案:

答案 0 :(得分:0)

你应该初始化你的i: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
            }
        }
    }

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

答案 1 :(得分:0)

首先是:

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

将完成这项工作,而不是:

((NewFloor > 7) || (NewFloor < 0) || (NewFloor == 7))

其次你可以这样开始:

 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;