混淆,为什么不能调用”void passenger”,而且没有任何错误。

huangapple go评论76阅读模式
英文:

Confusing, why not able to call out void passenger and there is no any error

问题

import java.util.Scanner;

public class Elevator {
    private int currentFloor;
    private static final int MAXFLOOR = 15;
    int newFloor;
    int newP;
    int currentP = 1;
    int maxP = 20;

    public Elevator() {
        currentFloor = 1;
    }

    public void passenger() {
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("Enter total passenger: ");
        newP = scanner1.nextInt();
        if (newP > maxP || newP == currentP) {
            System.out.println("Invalid passenger number");
            passenger();
        } else {
            if (newP < currentP) {
                minPassenger();
            } else if (newP > currentP) {
                maxPassenger();
            }
        }
        passenger();
    }

    public void maxPassenger() {
        System.out.println("Maximum passenger");
    }

    public void minPassenger() {
        System.out.println("0 passenger, not acceptable");
    }

    public void display(Object a) {
        System.out.println(a);
    }

    public void request(Object a) {
        System.out.println(a);
    }

    public void delay(int ms) {
        try {
            Thread.sleep(ms);
        } catch (Exception e) {
        }
    }

    public void moveUp() {
        System.out.println("Starting at floor " + currentFloor);
        while (currentFloor++ < newFloor) {
            System.out.println("Going up -  now at floor " + currentFloor);
            delay(600);
        }
        currentFloor--;
        System.out.println("Stopping at floor " + newFloor);
    }

    public void moveDown() {
        System.out.println("Starting at floor " + currentFloor);
        while (currentFloor-- > newFloor) {
            System.out.println("Going down -  now at floor " + currentFloor);
            delay(600);
        }
        currentFloor++;
        System.out.println("Stopping at floor " + newFloor);
    }

    public void request() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter new floor: ");
        newFloor = scanner.nextInt();
        if (newFloor > MAXFLOOR || newFloor == currentFloor) {
            System.out.println("INCORRECT FLOOR NUMBER, NO ACTION TAKEN");
            request();
        } else {
            if (newFloor < currentFloor) {
                moveDown();
            } else if (newFloor > currentFloor) {
                moveUp();
            }
        }
        request();
    }

    public static void main(String[] args) {
        Elevator e = new Elevator();
        e.request();
        e.passenger();
    }
}
英文:

I am new learner to Java. Need some advise from expert. I want to make the program to ask how many passenger when every selected floor reached. I notice if I removed the call request the loop will just work for one time. But I need it to keep repeat and repeat. Anyways to make it happen without remove any current other methods? Thanks.

import java.util.Scanner;

public class Elevator {
    private int currentFloor;
    private static final int MAXFLOOR = 15;
    int newFloor;
    int newP;
    int currentP = 1;
    int maxP = 20;

    public Elevator() {
        currentFloor = 1;
    }

    public void passenger() {
        Scanner scanner1 = new Scanner(System.in);
        System.out.println(&quot;Enter total passenger: &quot;);
        newP = scanner1.nextInt();
        if (newP &gt; maxP || newP == currentP) {
            System.out.println(&quot;Invalid passenger number&quot;);
            passenger();
        }
        else {
            if (newP &lt; currentP) {
                minPassenger();
            }
            else if (newP &gt; currentP) {
                maxPassenger();

            }
        }
        passenger();
    }

    public void maxPassenger() {
        System.out.println(&quot;Maximum passenger&quot;);
    }

    public void minPassenger() {
        System.out.println(&quot;0 passenger, not acceptable&quot;);
    }

    public void display(Object a) {
        System.out.println(a);
    }

    public void request(Object a) {
        System.out.println(a);
    }

    public void delay(int ms) {
        try {
            Thread.sleep(ms);
        }
        catch (Exception e) {
        }
    }

    public void moveUp() {
        System.out.println(&quot;Starting at floor &quot; + currentFloor);
        while (currentFloor++ &lt; newFloor) {
            System.out.println(&quot;Going up -  now at floor &quot; + currentFloor);
            delay(600);
        }
        currentFloor--;
        System.out.println(&quot;Stopping at floor &quot; + newFloor);
    }

    public void moveDown() {
        System.out.println(&quot;Starting at floor &quot; + currentFloor);
        while (currentFloor-- &gt; newFloor) {
            System.out.println(&quot;Going down -  now at floor &quot; + currentFloor);
            delay(600);
        }
        currentFloor++;
        System.out.println(&quot;Stopping at floor &quot; + newFloor);
    }

    public void request() {
        Scanner scanner = new Scanner(System.in);
        System.out.println(&quot;Enter new floor: &quot;);
        newFloor = scanner.nextInt();
        if (newFloor &gt; MAXFLOOR || newFloor == currentFloor) {
            System.out.println(&quot;INCORRECT FLOOR NUMBER, NO ACTION TAKEN&quot;);
            request();
        }
        else {

            if (newFloor &lt; currentFloor) {
                moveDown();
            }
            else if (newFloor &gt; currentFloor) {
                moveUp();

            }
        }
        request();
    }

    public static void main(String[] args) {
        Elevator e = new Elevator();
        e.request();
        e.passenger();
    }
}

答案1

得分: 0

这是您的 main() 方法。

public static void main(String[] args) {
    Elevator e = new Elevator();
    while (e.currentFloor >= 0) {
        e.request();
        if (e.currentFloor >= 0) {
            e.passenger();
        }
    }
}
英文:

Here is your main() method.

public static void main(String[] args) {
    Elevator e = new Elevator();
    e.request();
    e.passenger();
}

Method request() never ends, because the last line in that method is a call to itself. Hence the line e.passenger() is never executed.

You should put a while loop in method main() and decide on a way to terminate the loop, for example entering a floor which is negative. And remove the last line in method request() &ndash; likewise with method passenger(), remove the last line where you call the method again.

I believe the following code does what you want. Note that you only need to create a Scanner object once.

import java.util.Scanner;

public class Elevator {
    private Scanner scanner;
    private int currentFloor;
    private static final int MAXFLOOR = 15;
    int newFloor;
    int newP;
    int currentP = 1;
    int maxP = 20;

    public Elevator() {
        currentFloor = 1;
        scanner = new Scanner(System.in);
    }

    public void passenger() {
        System.out.print(&quot;Enter total passenger: &quot;);
        newP = scanner.nextInt();
        if (newP &gt; maxP || newP == currentP) {
            System.out.println(&quot;Invalid passenger number&quot;);
            passenger();
        }
        else {
            if (newP &lt; currentP) {
                minPassenger();
                passenger();
            }
            else if (newP &gt; currentP) {
                maxPassenger();
            }
        }
    }

    public void maxPassenger() {
        System.out.println(&quot;Maximum passenger&quot;);
    }

    public void minPassenger() {
        System.out.println(&quot;0 passenger, not acceptable&quot;);
    }

    public void display(Object a) {
        System.out.println(a);
    }

    public void request(Object a) {
        System.out.println(a);
    }

    public void delay(int ms) {
        try {
            Thread.sleep(ms);
        }
        catch (Exception e) {
        }
    }

    public void moveUp() {
        System.out.println(&quot;Starting at floor &quot; + currentFloor);
        while (currentFloor++ &lt; newFloor) {
            System.out.println(&quot;Going up -  now at floor &quot; + currentFloor);
            delay(600);
        }
        currentFloor--;
        System.out.println(&quot;Stopping at floor &quot; + newFloor);
    }

    public void moveDown() {
        System.out.println(&quot;Starting at floor &quot; + currentFloor);
        while (currentFloor-- &gt; newFloor) {
            System.out.println(&quot;Going down -  now at floor &quot; + currentFloor);
            delay(600);
        }
        currentFloor++;
        System.out.println(&quot;Stopping at floor &quot; + newFloor);
    }

    public void request() {
        System.out.print(&quot;Enter new floor: &quot;);
        newFloor = scanner.nextInt();
        if (newFloor &lt; 0) {
            currentFloor = newFloor;
            return;
        }
        if (newFloor &gt; MAXFLOOR || newFloor == currentFloor) {
            System.out.println(&quot;INCORRECT FLOOR NUMBER, NO ACTION TAKEN&quot;);
            request();
        }
        else {
            if (newFloor &lt; currentFloor) {
                moveDown();
            }
            else if (newFloor &gt; currentFloor) {
                moveUp();
            }
        }
    }

    public static void main(String[] args) {
        Elevator e = new Elevator();
        while (e.currentFloor &gt;= 0) {
            e.request();
            if (e.currentFloor &gt;= 0) {
                e.passenger();
            }
        }
    }
}

huangapple
  • 本文由 发表于 2020年10月24日 16:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64511354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定