一个包含for循环和while循环的ArrayList。它应该是语句的另一种顺序。

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

an ArrayList with for and while loops. It should be another order of statements

问题

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. List<Car> cars = new ArrayList<>();
  6. while (scan.hasNext()) {
  7. Car car = new Car();
  8. String make = scan.nextLine();
  9. if ("-1".equals(make)) break;
  10. cars.add(car);
  11. String model = scan.nextLine();
  12. if ("-1".equals(model)) break;
  13. car.setMake(make);
  14. car.setModel(model);
  15. }
  16. scan.close();
  17. for (Car car : cars) {
  18. System.out.println(car.getMake() + " " + car.getModel());
  19. }
  20. System.out.format("There are %s cars \n", cars.size());
  21. }
  22. }
  1. public class Car {
  2. private String make;
  3. private String model;
  4. public Car() {
  5. // You need to remove the constructor parameters
  6. }
  7. public String getMake() {
  8. return make;
  9. }
  10. public void setMake(String make) {
  11. this.make = make;
  12. }
  13. public String getModel() {
  14. return model;
  15. }
  16. public void setModel(String model) {
  17. this.model = model;
  18. }
  19. }
  1. 编写一个程序,用于将汽车的制造商和型号填充到ArrayList中。
  2. 程序应该输出ArrayList中所有汽车的制造商和型号。
  3. 输入:
  4. Honda
  5. Fit
  6. Toyota
  7. Corolla
  8. Mazda
  9. Axela
  10. -1
  11. 最后,您的程序应该将ArrayList中所有汽车的制造商和型号输出。程序将此打印到标准输出:
  12. 共有3辆汽车
  13. Honda Fit
  14. Toyota Corolla
  15. Mazda Axela

在Car类的构造函数中,应该移除构造函数参数,因为你在构造函数中没有使用它们。

英文:
  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. List &lt; Car &gt; cars = new ArrayList &lt; &gt; ();
  6. while (scan.hasNext()) {
  7. Car car = new Car();
  8. String make = scan.nextLine();
  9. if (&quot;-1&quot;.equals(make)) break;
  10. cars.add(car);
  11. String model = scan.nextLine();
  12. if (&quot;-1&quot;.equals(model)) break;
  13. car.setMake(make);
  14. car.setModel(model);
  15. }
  16. scan.close();
  17. for (Car car: cars) {
  18. System.out.println(car.getMake() + &quot; &quot; + car.getModel());
  19. System.out.format(&quot;There are %s cars \n&quot;, cars.size());
  20. }
  21. System.out.format(&quot;%s&quot;, &quot;\n&quot;);
  22. }
  23. }
  1. public class Car {
  2. private String make;
  3. private String model;
  4. public Car() {
  5. this.make = make;
  6. this.model = model;
  7. }
  8. public String getMake() {
  9. return make;
  10. }
  11. public void setMake(String make) {
  12. this.make = make;
  13. }
  14. public String getModel() {
  15. return model;
  16. }
  17. public void setModel(String model) {
  18. this.model = model;
  19. }
  20. }

a program that populates an ArrayList with the make and model of cars.

program should output the makes and models of all cars in the ArrayList.

input:

Honda
Fit
Toyota
Corolla
Mazda
Axela
-1

Finally, your program should output the makes and models of all cars in the ArrayList. The program prints this to standard output:

There are 3 cars
Honda Fit
Toyota Corolla
Mazda Axela

Where I have an error? It should be another order of statements.

答案1

得分: 0

以下是您要翻译的内容:

起初你的程序中出现的错误是使用了 Scanner#hasNext 方法。

虽然你的概念是正确的,但在读取标准输入时,这不是你想要使用的方法。

实际上,Scanner 会无限等待更多的输入。因此,它不能用于确定用户是否已完成输入值。

将其更改为以下内容。

  1. Car car;
  2. String make, model;
  3. do {
  4. car = new Car();
  5. car.setMake(make = scan.nextLine());
  6. if (make.isEmpty()) break;
  7. car.setModel(model = scan.nextLine());
  8. cars.add(car);
  9. } while (!model.isEmpty());

此外,由于我们现在正在使用该名称,您需要更改后续的 for-loop 变量名称为 car

输出

  1. Honda
  2. Fit
  3. Toyota
  4. Corolla
  5. Mazda
  6. Axela
  7. Honda Fit
  8. 3辆车
  9. Toyota Corolla
  10. 3辆车
  11. Mazda Axela
  12. 3辆车

以下是您的代码可以进行的一些改进。

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. List<Car> cars = new ArrayList<>();
  4. String make, model;
  5. do {
  6. make = scan.nextLine();
  7. if (make.isEmpty()) break;
  8. model = scan.nextLine();
  9. cars.add(new Car(make, model));
  10. } while (!model.isEmpty());
  11. scan.close();
  12. for (Car car : cars) System.out.println(car);
  13. System.out.format("有%s辆车\n", cars.size());
  14. System.out.format("%s", "\n");
  15. }
  1. public class Car {
  2. private String make, model;
  3. public Car(String make, String model) {
  4. this.make = make;
  5. this.model = model;
  6. }
  7. public String getMake() {
  8. return make;
  9. }
  10. public void setMake(String make) {
  11. this.make = make;
  12. }
  13. public String getModel() {
  14. return model;
  15. }
  16. public void setModel(String model) {
  17. this.model = model;
  18. }
  19. @Override
  20. public String toString() {
  21. return make + " " + model;
  22. }
  23. }
英文:

The initial error with your program is the use of the Scanner#hasNext method.

While your concept is correct, this is not the method you'll want to use when reading standard-in input.

Essentially, the Scanner will wait for more input, indefinitely.
Thus, it is not a way to determine if the user has completed entering values.

Change this to the following.

  1. Car car;
  2. String make, model;
  3. do {
  4. car = new Car();
  5. car.setMake(make = scan.nextLine());
  6. if (make.isEmpty()) break;
  7. car.setModel(model = scan.nextLine());
  8. cars.add(car);
  9. } while (!model.isEmpty());

Additionally, you need to change the subsequent for-loop variable name, car, since we are now using that name.

Output

  1. Honda
  2. Fit
  3. Toyota
  4. Corolla
  5. Mazda
  6. Axela
  7. Honda Fit
  8. There are 3 cars
  9. Toyota Corolla
  10. There are 3 cars
  11. Mazda Axela
  12. There are 3 cars

Here are a few improvements you can make to your code.

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. List&lt;Car&gt; cars = new ArrayList&lt;&gt;();
  4. String make, model;
  5. do {
  6. make = scan.nextLine();
  7. if (make.isEmpty()) break;
  8. model = scan.nextLine();
  9. cars.add(new Car(make, model));
  10. } while (!model.isEmpty());
  11. scan.close();
  12. for (Car car : cars) System.out.println(car);
  13. System.out.format(&quot;There are %s cars \n&quot;, cars.size());
  14. System.out.format(&quot;%s&quot;, &quot;\n&quot;);
  15. }
  1. public class Car {
  2. private String make, model;
  3. public Car(String make, String model) {
  4. this.make = make;
  5. this.model = model;
  6. }
  7. public String getMake() {
  8. return make;
  9. }
  10. public void setMake(String make) {
  11. this.make = make;
  12. }
  13. public String getModel() {
  14. return model;
  15. }
  16. public void setModel(String model) {
  17. this.model = model;
  18. }
  19. @Override
  20. public String toString() {
  21. return make + &quot; &quot; + model;
  22. }
  23. }

huangapple
  • 本文由 发表于 2023年6月15日 08:07:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478301.html
匿名

发表评论

匿名网友

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

确定