我正在做作业,在作业中我需要创建一个Java ArrayList来保存从扫描仪读取的多个变量类型,以描述有关车辆的详细信息(例如,车辆的制造、型号、颜色、年份、里程)。这应该是一个车辆库存应用程序。实际上,我很难接受用户输入的信息,并将其添加到ArrayList中,这样我就可以将ArrayList打印到控制台,以读取用户输入的内容。
这是我第一次不得不用多个变量类型(String,int)来编写ArrayList,所以我很难在一个地方获得所有的信息。
谢谢大家的帮助--我对编程非常陌生,我很感激你能给我的任何见解来改进我的代码。
import java.util.ArrayList;
import java.util.Scanner;
/*
* This is a program that will store and allow
* the user to manipulate information regarding
* a vehicle in an inventory
*/
public class Automobileinventory {
public static void main(String[] args) {
Automobile automobile = new Automobile();
addVehicle();
}
public static void addVehicle() {
Scanner scnr = new Scanner(System.in);
String make;
String model;
String color;
int year;
int mileage;
ArrayList<Automobile> list = new ArrayList<Automobile> ();
list.add (new Automobile());
System.out.println("Enter vehicle make:");
make = scnr.nextLine();
System.out.println("Enter vehicle model:");
model = scnr.nextLine();
System.out.println("Enter vehicle color:");
color = scnr.nextLine();
System.out.println("Enter vehicle year:");
year = scnr.nextInt();
System.out.println("Enter vehicle mileage");
mileage = scnr.nextInt();
Automobile c = new Automobile ();
list.add(c);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}输出:
现在--当我运行代码时,我得到的是: 输入车辆型号: Pontiac 进入车辆制造: Grand 输入车辆颜色:绿色 输入车辆年份:1997年 输入车辆里程200000 汽车@55f96302汽车@3d4eac69
守则应返回:
预期输出:
输入车辆型号: Pontiac 输入车辆型号: Grand Am 输入车辆颜色:绿色 输入车辆年份:1997年 输入车辆里程200000 庞蒂亚克大酒店绿色200000
发布于 2018-12-31 00:42:08
你就快到了,但有了几处变化,
所有属性
String make,String model,String color, int year,int mileage都应该是带有Getters and Setters的Automobile类的一部分。
class Automobile{
String make;
String model;
String color;
int year;
int mileage;
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 String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
}用期望的输出、示例重写
Automobile类的Automobile方法
@Override
public String toString() {
return "Automobile [make=" + make + ", model=" + model + ", color=" + color + ", year=" + year
+ ", mileage=" + mileage + "]";
}在
addVehicle中,始终为每个车辆创建Automobile对象,并设置所有这些属性,然后将其添加到列表中
public static void addVehicle() {
Scanner scnr = new Scanner(System.in);
ArrayList<Automobile> list = new ArrayList<Automobile> ();
Automobile automobile = new Automobile();
System.out.println("Enter vehicle make:");
automobile.setMake(scnr.nextLine());
System.out.println("Enter vehicle model:");
automobile.setModel(scnr.nextLine());
System.out.println("Enter vehicle color:");
automobile.setColor(scnr.nextLine());
System.out.println("Enter vehicle year:");
automobile.setYear(scnr.nextInt());
System.out.println("Enter vehicle mileage");
automobile.setMileage(scnr.nextInt());
list.add(automobile);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i)); //this will print the tostring representation of automobile object
}
}发布于 2018-12-31 00:34:59
要做到这一点,您需要重写您的toString类的Automobile方法:
private static final String SEPARATOR = " ";
@Override
public String toString() {
return new StringBuilder(getModel())
.append(SEPARATOR)
.append(getMake())
.append(SEPARATOR)
.append(getColor())
.append(SEPARATOR)
.append(getYear())
.append(SEPARATOR)
.append(getMileage())
.toString();
}在这里您可以找到文档 of Object.toString
按照文件规定,该方法的目的:
返回对象的字符串表示形式。通常,toString方法返回“文本表示”此对象的字符串。其结果应该是一个简洁但内容丰富的表示形式,对于一个人来说,很容易阅读。建议所有子类重写此方法。
发布于 2018-12-31 00:44:20
代码中存在一些问题:
所有的信息都是正确的,但是要将它们存储在辅助变量中,并且在添加到列表之前永远不要进入c。你需要做的是:
Automobile c = new Automobile ();
System.out.println("Enter vehicle make:");
c.make = scnr.nextLine();
System.out.println("Enter vehicle model:");
c.model = scnr.nextLine();
System.out.println("Enter vehicle color:");
c.color = scnr.nextLine();
System.out.println("Enter vehicle year:");
c.year = scnr.nextInt();
System.out.println("Enter vehicle mileage");
c.mileage = scnr.nextInt();
list.add(c);第二个问题是
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}System.out.println不打印像魔术这样的任何类型的对象,您需要传递一些字符串(如i.name )才能打印。如果您像在上面的代码中所做的那样传递,您只是传递一个要打印的指针,并且可以在输出中看到java adress引用。
试着用一些好的方法,比如封装。
祝你好运,欢迎来到编程世界。
编辑:
注释中的其他人员展示了如何使对象可由system.out.println打印,并且是输出的解决方案之一。作出更正,以确保您正在存储的信息正确,他们尝试该方法。
https://stackoverflow.com/questions/53982553
复制相似问题