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

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

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

问题

  1. import java.util.Scanner;
  2. public class Elevator {
  3. private int currentFloor;
  4. private static final int MAXFLOOR = 15;
  5. int newFloor;
  6. int newP;
  7. int currentP = 1;
  8. int maxP = 20;
  9. public Elevator() {
  10. currentFloor = 1;
  11. }
  12. public void passenger() {
  13. Scanner scanner1 = new Scanner(System.in);
  14. System.out.println("Enter total passenger: ");
  15. newP = scanner1.nextInt();
  16. if (newP > maxP || newP == currentP) {
  17. System.out.println("Invalid passenger number");
  18. passenger();
  19. } else {
  20. if (newP < currentP) {
  21. minPassenger();
  22. } else if (newP > currentP) {
  23. maxPassenger();
  24. }
  25. }
  26. passenger();
  27. }
  28. public void maxPassenger() {
  29. System.out.println("Maximum passenger");
  30. }
  31. public void minPassenger() {
  32. System.out.println("0 passenger, not acceptable");
  33. }
  34. public void display(Object a) {
  35. System.out.println(a);
  36. }
  37. public void request(Object a) {
  38. System.out.println(a);
  39. }
  40. public void delay(int ms) {
  41. try {
  42. Thread.sleep(ms);
  43. } catch (Exception e) {
  44. }
  45. }
  46. public void moveUp() {
  47. System.out.println("Starting at floor " + currentFloor);
  48. while (currentFloor++ < newFloor) {
  49. System.out.println("Going up - now at floor " + currentFloor);
  50. delay(600);
  51. }
  52. currentFloor--;
  53. System.out.println("Stopping at floor " + newFloor);
  54. }
  55. public void moveDown() {
  56. System.out.println("Starting at floor " + currentFloor);
  57. while (currentFloor-- > newFloor) {
  58. System.out.println("Going down - now at floor " + currentFloor);
  59. delay(600);
  60. }
  61. currentFloor++;
  62. System.out.println("Stopping at floor " + newFloor);
  63. }
  64. public void request() {
  65. Scanner scanner = new Scanner(System.in);
  66. System.out.println("Enter new floor: ");
  67. newFloor = scanner.nextInt();
  68. if (newFloor > MAXFLOOR || newFloor == currentFloor) {
  69. System.out.println("INCORRECT FLOOR NUMBER, NO ACTION TAKEN");
  70. request();
  71. } else {
  72. if (newFloor < currentFloor) {
  73. moveDown();
  74. } else if (newFloor > currentFloor) {
  75. moveUp();
  76. }
  77. }
  78. request();
  79. }
  80. public static void main(String[] args) {
  81. Elevator e = new Elevator();
  82. e.request();
  83. e.passenger();
  84. }
  85. }
英文:

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.

  1. import java.util.Scanner;
  2. public class Elevator {
  3. private int currentFloor;
  4. private static final int MAXFLOOR = 15;
  5. int newFloor;
  6. int newP;
  7. int currentP = 1;
  8. int maxP = 20;
  9. public Elevator() {
  10. currentFloor = 1;
  11. }
  12. public void passenger() {
  13. Scanner scanner1 = new Scanner(System.in);
  14. System.out.println(&quot;Enter total passenger: &quot;);
  15. newP = scanner1.nextInt();
  16. if (newP &gt; maxP || newP == currentP) {
  17. System.out.println(&quot;Invalid passenger number&quot;);
  18. passenger();
  19. }
  20. else {
  21. if (newP &lt; currentP) {
  22. minPassenger();
  23. }
  24. else if (newP &gt; currentP) {
  25. maxPassenger();
  26. }
  27. }
  28. passenger();
  29. }
  30. public void maxPassenger() {
  31. System.out.println(&quot;Maximum passenger&quot;);
  32. }
  33. public void minPassenger() {
  34. System.out.println(&quot;0 passenger, not acceptable&quot;);
  35. }
  36. public void display(Object a) {
  37. System.out.println(a);
  38. }
  39. public void request(Object a) {
  40. System.out.println(a);
  41. }
  42. public void delay(int ms) {
  43. try {
  44. Thread.sleep(ms);
  45. }
  46. catch (Exception e) {
  47. }
  48. }
  49. public void moveUp() {
  50. System.out.println(&quot;Starting at floor &quot; + currentFloor);
  51. while (currentFloor++ &lt; newFloor) {
  52. System.out.println(&quot;Going up - now at floor &quot; + currentFloor);
  53. delay(600);
  54. }
  55. currentFloor--;
  56. System.out.println(&quot;Stopping at floor &quot; + newFloor);
  57. }
  58. public void moveDown() {
  59. System.out.println(&quot;Starting at floor &quot; + currentFloor);
  60. while (currentFloor-- &gt; newFloor) {
  61. System.out.println(&quot;Going down - now at floor &quot; + currentFloor);
  62. delay(600);
  63. }
  64. currentFloor++;
  65. System.out.println(&quot;Stopping at floor &quot; + newFloor);
  66. }
  67. public void request() {
  68. Scanner scanner = new Scanner(System.in);
  69. System.out.println(&quot;Enter new floor: &quot;);
  70. newFloor = scanner.nextInt();
  71. if (newFloor &gt; MAXFLOOR || newFloor == currentFloor) {
  72. System.out.println(&quot;INCORRECT FLOOR NUMBER, NO ACTION TAKEN&quot;);
  73. request();
  74. }
  75. else {
  76. if (newFloor &lt; currentFloor) {
  77. moveDown();
  78. }
  79. else if (newFloor &gt; currentFloor) {
  80. moveUp();
  81. }
  82. }
  83. request();
  84. }
  85. public static void main(String[] args) {
  86. Elevator e = new Elevator();
  87. e.request();
  88. e.passenger();
  89. }
  90. }

答案1

得分: 0

这是您的 main() 方法。

  1. public static void main(String[] args) {
  2. Elevator e = new Elevator();
  3. while (e.currentFloor >= 0) {
  4. e.request();
  5. if (e.currentFloor >= 0) {
  6. e.passenger();
  7. }
  8. }
  9. }
英文:

Here is your main() method.

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

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.

  1. import java.util.Scanner;
  2. public class Elevator {
  3. private Scanner scanner;
  4. private int currentFloor;
  5. private static final int MAXFLOOR = 15;
  6. int newFloor;
  7. int newP;
  8. int currentP = 1;
  9. int maxP = 20;
  10. public Elevator() {
  11. currentFloor = 1;
  12. scanner = new Scanner(System.in);
  13. }
  14. public void passenger() {
  15. System.out.print(&quot;Enter total passenger: &quot;);
  16. newP = scanner.nextInt();
  17. if (newP &gt; maxP || newP == currentP) {
  18. System.out.println(&quot;Invalid passenger number&quot;);
  19. passenger();
  20. }
  21. else {
  22. if (newP &lt; currentP) {
  23. minPassenger();
  24. passenger();
  25. }
  26. else if (newP &gt; currentP) {
  27. maxPassenger();
  28. }
  29. }
  30. }
  31. public void maxPassenger() {
  32. System.out.println(&quot;Maximum passenger&quot;);
  33. }
  34. public void minPassenger() {
  35. System.out.println(&quot;0 passenger, not acceptable&quot;);
  36. }
  37. public void display(Object a) {
  38. System.out.println(a);
  39. }
  40. public void request(Object a) {
  41. System.out.println(a);
  42. }
  43. public void delay(int ms) {
  44. try {
  45. Thread.sleep(ms);
  46. }
  47. catch (Exception e) {
  48. }
  49. }
  50. public void moveUp() {
  51. System.out.println(&quot;Starting at floor &quot; + currentFloor);
  52. while (currentFloor++ &lt; newFloor) {
  53. System.out.println(&quot;Going up - now at floor &quot; + currentFloor);
  54. delay(600);
  55. }
  56. currentFloor--;
  57. System.out.println(&quot;Stopping at floor &quot; + newFloor);
  58. }
  59. public void moveDown() {
  60. System.out.println(&quot;Starting at floor &quot; + currentFloor);
  61. while (currentFloor-- &gt; newFloor) {
  62. System.out.println(&quot;Going down - now at floor &quot; + currentFloor);
  63. delay(600);
  64. }
  65. currentFloor++;
  66. System.out.println(&quot;Stopping at floor &quot; + newFloor);
  67. }
  68. public void request() {
  69. System.out.print(&quot;Enter new floor: &quot;);
  70. newFloor = scanner.nextInt();
  71. if (newFloor &lt; 0) {
  72. currentFloor = newFloor;
  73. return;
  74. }
  75. if (newFloor &gt; MAXFLOOR || newFloor == currentFloor) {
  76. System.out.println(&quot;INCORRECT FLOOR NUMBER, NO ACTION TAKEN&quot;);
  77. request();
  78. }
  79. else {
  80. if (newFloor &lt; currentFloor) {
  81. moveDown();
  82. }
  83. else if (newFloor &gt; currentFloor) {
  84. moveUp();
  85. }
  86. }
  87. }
  88. public static void main(String[] args) {
  89. Elevator e = new Elevator();
  90. while (e.currentFloor &gt;= 0) {
  91. e.request();
  92. if (e.currentFloor &gt;= 0) {
  93. e.passenger();
  94. }
  95. }
  96. }
  97. }

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:

确定