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

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

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

问题

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List<Car> cars = new ArrayList<>();
        while (scan.hasNext()) {
            Car car = new Car();
            String make = scan.nextLine();
            if ("-1".equals(make)) break;
            cars.add(car);
            String model = scan.nextLine();
            if ("-1".equals(model)) break;
            car.setMake(make);
            car.setModel(model);
        }
        scan.close();
        for (Car car : cars) {
            System.out.println(car.getMake() + " " + car.getModel());
        }
        System.out.format("There are %s cars \n", cars.size());
    }
}
public class Car {
    private String make;
    private String model;

    public Car() {
        // You need to remove the constructor parameters
    }

    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}
编写一个程序,用于将汽车的制造商和型号填充到ArrayList中。

程序应该输出ArrayList中所有汽车的制造商和型号。

输入:

Honda
Fit
Toyota
Corolla
Mazda
Axela
-1

最后,您的程序应该将ArrayList中所有汽车的制造商和型号输出。程序将此打印到标准输出:

共有3辆汽车
Honda Fit
Toyota Corolla
Mazda Axela

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

英文:
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List &lt; Car &gt; cars = new ArrayList &lt; &gt; ();
        while (scan.hasNext()) {
            Car car = new Car();
            String make = scan.nextLine();
            if (&quot;-1&quot;.equals(make)) break;
            cars.add(car);
            String model = scan.nextLine();
            if (&quot;-1&quot;.equals(model)) break;
            car.setMake(make);
            car.setModel(model);
        }
        scan.close();
        for (Car car: cars) {

            System.out.println(car.getMake() + &quot; &quot; + car.getModel());

            System.out.format(&quot;There are %s cars \n&quot;, cars.size());
        }
        System.out.format(&quot;%s&quot;, &quot;\n&quot;);
    }
}
public class Car {
    private String make;
    private String model;

    public Car() {
        this.make = make;
        this.model = model;
    }

    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

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 会无限等待更多的输入。因此,它不能用于确定用户是否已完成输入值。

将其更改为以下内容。

Car car;
String make, model;
do {
    car = new Car();
    car.setMake(make = scan.nextLine());
    if (make.isEmpty()) break;
    car.setModel(model = scan.nextLine());
    cars.add(car);
} while (!model.isEmpty());

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

输出

Honda
Fit
Toyota
Corolla
Mazda
Axela

Honda Fit
有3辆车
Toyota Corolla
有3辆车
Mazda Axela
有3辆车

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

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    List<Car> cars = new ArrayList<>();
    String make, model;
    do {
        make = scan.nextLine();
        if (make.isEmpty()) break;
        model = scan.nextLine();
        cars.add(new Car(make, model));
    } while (!model.isEmpty());
    scan.close();
    for (Car car : cars) System.out.println(car);
    System.out.format("有%s辆车\n", cars.size());
    System.out.format("%s", "\n");
}
public class Car {
    private String make, model;

    public Car(String make, String model) {
        this.make = make;
        this.model = model;
    }

    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @Override
    public String toString() {
        return make + " " + model;
    }
}
英文:

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.

Car car;
String make, model;
do {
    car = new Car();
    car.setMake(make = scan.nextLine());
    if (make.isEmpty()) break;
    car.setModel(model = scan.nextLine());
    cars.add(car);
} while (!model.isEmpty());

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

Output

Honda
Fit
Toyota
Corolla
Mazda
Axela

Honda Fit
There are 3 cars 
Toyota Corolla
There are 3 cars 
Mazda Axela
There are 3 cars 

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

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    List&lt;Car&gt; cars = new ArrayList&lt;&gt;();
    String make, model;
    do {
        make = scan.nextLine();
        if (make.isEmpty()) break;
        model = scan.nextLine();
        cars.add(new Car(make, model));
    } while (!model.isEmpty());
    scan.close();
    for (Car car : cars) System.out.println(car);
    System.out.format(&quot;There are %s cars \n&quot;, cars.size());
    System.out.format(&quot;%s&quot;, &quot;\n&quot;);
}
public class Car {
    private String make, model;

    public Car(String make, String model) {
        this.make = make;
        this.model = model;
    }

    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @Override
    public String toString() {
        return make + &quot; &quot; + model;
    }
}

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:

确定