java - Java如何将用户输入保存在数组中
问题描述
不确定如何将用户输入保存到数组 reg[i] 中。我如何将它们保存到 reg[i] 中,以便我可以通过 toString 方法打印出来。
包含4个java类。所有者,汽车(汽车类型),汽车类型和注册(所有者,汽车)。
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
CarType[] type = {new CarType("Toyota", "Vios", 1.5)
, new CarType("Nissan","Teana",2.0)
, new CarType("Honda ","City", 1.6)};
Register[] reg = new Register[3];
for(int i=0; i<reg.length; i++){
//registration Number
System.out.println("Registration Numnber" + Register.getnextRegNo());
//owner name and ic
System.out.print("Enter owner's name: ");
String name = in.nextLine();
System.out.print("Enter IC number: ");
String ICNo = in.next();
Owner owner= new Owner(name, ICNo);
//car info
System.out.print("Enter plate No: ");
String plateNo = in.next();
System.out.print("Enter color: ");
String color = in.next();
System.out.print("Enter year: ");
int year = in.nextInt();
//car type choose
System.out.println("\nCar Types:");
for(int j=0; j<type.length; ++j){
System.out.println((j+1)+"."+type[j]);
}
System.out.println("\nSelect car type[1...3]: ");
int select = in.nextInt();
CarType carType = type[select-1];
Car car=new Car(plateNo, color, year, carType);
//my thought is add some code here to save the inputs
}
System.out.println("\t\t\t\t\tCar Registration Listing");
System.out.println("Reg No.\tName\t\tIC No.\t\tPlate No.\tColor\tYear\tMake\tModel\tCapacity");
for(int k=0; k<reg.length; k++){
System.out.println(reg[k].toString());
}
}
解决方案
假设Register
该类采用Owner
and Car
in 构造函数参数。
你已经reg
正确地声明了数组,现在你只需要给它赋值。
你可以这样做
reg[i] = new Register(owner, car);
推荐阅读
- .htaccess - 将所有 http 重定向到 https,将所有 www 重定向到非 www,并从所有 url 中删除 .html
- php - PHP(Codeigniter 3)会话在Google Cloud Platform中重定向后被破坏,但在Localhost和Cpanel中正常工作
- python - python中向量的大小是(N,1)而不是(N,)
- lua - 如何在lua中将一个表存储到另一个表?
- python - 将 hachoir 库的衍生物安装到 Google Colab 中
- mysql - 在 MySQL 中获取“错误 1452(23000):无法添加或更新子行”
- javascript - 为什么 Google Maps .getCenter() 返回无效坐标?
- javascript - GSAP + AdHelper 模板导致“未定义 lib”
- php - 在 Heroku 上安装 PECL 库
- deep-learning - 使用参数共享计算卷积神经网络中的权重数量