繁体   English   中英

如何在数组/ arraylist中存储多个用户输入(String,Int,Double)并输出存储的数据(Java)?

[英]How do i store multiple user inputs (String,Int,Double) in an array/arraylist and output the stored data(Java)?

我正在使用存储在数组和/或arraylist中的用户输入来研究Car Garage。 这些至少是提供的具体指示。 我是编程新手,缺乏一些技术语言。 感谢您的帮助。

当前问题:我可以接受用户输入/输入,但最后输入的车将是唯一保存和输出的车。

public class Car {

    String make;
    String model;
    int year;
    double price;

    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;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }   
}

public class CarApp {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    char userCon = 'Y';

    System.out.println("Welcome to the Garage!");

    while(userCon =='Y') {
        System.out.println("How many cars will you be entering?");
            int carAmount = scan.nextInt();
            ArrayList<Car> car = new ArrayList<Car>(carAmount);
            Car carMake = new Car();
            Car carYear = new Car();
            Car carModel = new Car();
            Car carPrice = new Car();

        for(int i =0;i<carAmount;i++) {
            System.out.println("Enter the make of the car " + (i + 1) + ":");
            carMake.make = scan.next();

            System.out.println("Enter the model of the car: ");
            carModel.model = scan.next();
            System.out.println("Enter the year: ");
            carYear.year = scan.nextInt();
            System.out.println("Enter the price: ");
            carPrice.price = scan.nextDouble();

            System.out.println("Thank you, Car " + (i + 1) + " is set!");

    }       
        System.out.println("The garage is holding " + carAmount + " cars.");
        for(int n =0;n<carAmount;n++) {
            System.out.println("Make: " + carMake.getMake());
            System.out.println("Model: " + carModel.getModel());
            System.out.println("Year: " + carYear.getYear());
            System.out.println("Price: " + carModel.getPrice());

    }   
        System.out.println("Would you like to try again? Y/N");
        String word = scan.next();
        word = word.toUpperCase();
        userCon = word.charAt(0);
        }
        scan.close();
    }
}
public class CarApp {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        char userCon = 'Y';

        System.out.println("Welcome to the Garage!");

        while(userCon =='Y') {
            System.out.println("How many cars will you be entering?");
            int carAmount = scan.nextInt();
            ArrayList<Car> carList = new ArrayList<Car>(carAmount);

            for(int i =0;i<carAmount;i++) {
                Car car = new Car();
                System.out.println("Enter the make of the car " + (i + 1) + ":");
                car.make = scan.next();

                System.out.println("Enter the model of the car: ");
                car.model = scan.next();
                System.out.println("Enter the year: ");
                car.year = scan.nextInt();
                System.out.println("Enter the price: ");
                car.price = scan.nextDouble();

                System.out.println("Thank you, Car " + (i + 1) + " is set!");
                carList.add(car);
            }
            System.out.println("The garage is holding " + carAmount + " cars.");
            for (Car car : carList) {
                System.out.println("Make: " + car.getMake());
                System.out.println("Model: " + car.getModel());
                System.out.println("Year: " + car.getYear());
                System.out.println("Price: " + car.getPrice());
            }

            System.out.println("Would you like to try again? Y/N");
            String word = scan.next();
            word = word.toUpperCase();
            userCon = word.charAt(0);
        }
        scan.close();
    }

您需要创建一个Car对象,并且需要将值分配给汽车的特定对象。 您在循环中执行此操作并将其添加到carList。 循环完成后,carList包含汽车列表。 您需要迭代该列表以打印您的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM