猿问

如何在数组/数组列表中存储多个用户输入(字符串、整数、双精度)并输出存储的数据(Java)?

我正在使用存储在数组和/或数组列表中的用户输入来学习汽车车库的课程。这些至少是提供的具体说明。我是编程新手,缺乏一些技术语言。提前感谢您的帮助。


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


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!");


    }       


交互式爱情
浏览 107回答 1
1回答

海绵宝宝撒

public class CarApp {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner scan = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; char userCon = 'Y';&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Welcome to the Garage!");&nbsp; &nbsp; &nbsp; &nbsp; while(userCon =='Y') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("How many cars will you be entering?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int carAmount = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Car> carList = new ArrayList<Car>(carAmount);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i =0;i<carAmount;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Car car = new Car();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the make of the car " + (i + 1) + ":");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; car.make = scan.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the model of the car: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; car.model = scan.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the year: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; car.year = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the price: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; car.price = scan.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Thank you, Car " + (i + 1) + " is set!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; carList.add(car);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The garage is holding " + carAmount + " cars.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Car car : carList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Make: " + car.getMake());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Model: " + car.getModel());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Year: " + car.getYear());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Price: " + car.getPrice());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Would you like to try again? Y/N");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String word = scan.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; word = word.toUpperCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userCon = word.charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; scan.close();&nbsp; &nbsp; }您需要创建一个 Car 对象,并且需要将值分配给汽车的特定对象。您在循环中执行此操作并将其添加到 carList。循环完成后, carList 包含汽车列表。您需要迭代该列表以打印您的数据。
随时随地看视频慕课网APP

相关分类

Java
我要回答