英文:
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("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();
}
}
答案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()
– 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("Enter total passenger: ");
newP = scanner.nextInt();
if (newP > maxP || newP == currentP) {
System.out.println("Invalid passenger number");
passenger();
}
else {
if (newP < currentP) {
minPassenger();
passenger();
}
else if (newP > currentP) {
maxPassenger();
}
}
}
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() {
System.out.print("Enter new floor: ");
newFloor = scanner.nextInt();
if (newFloor < 0) {
currentFloor = newFloor;
return;
}
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();
}
}
}
public static void main(String[] args) {
Elevator e = new Elevator();
while (e.currentFloor >= 0) {
e.request();
if (e.currentFloor >= 0) {
e.passenger();
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论