将对象添加到数组时出现问题 [英] Problem adding a object to an array

查看:61
本文介绍了将对象添加到数组时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我要使用的指示


要求用户输入商店中有效车辆的数量(这个数字应介于1和100之间,包括在内)。在由输入的有效车辆数量控制的循环中,要求用户输入每个车辆数据(即品牌,型号,年份和价格)。您可以假设所有输入数据都是正确的,无需进行数据验证。输入汽车的所有输入数据后,构造函数使用参数构建Car对象,然后将Car对象保存在数组中。


这是我到目前为止所拥有的:


/ * UMUC - CMIS 141 - 项目4

*文件:Car.java

*作者:Annie Ideus- Balboa

*日期:2007年5月13日

*完成时间:2007年5月17日

*修改为项目4:2007年5月29日

*使用指示:从命令窗口运行程序

* /


import java.util。* ;

导入java.net。*;

导入java.lang。*;


公共类汽车{


public static String make;

public static String model;

public static int year;

public static int price;


// Car();带参数的构造函数

public Car(String m,String d,int y,int p){

make = m;

model = d ;

年= y;

price = p;

} //结束构造函数


//返回make of make(make accessor)

public String getMake(){

return make;

} // end method


//返回汽车模型(模型访问者)

public String getModel(){

返回模型;

} //结束方法


//返回汽车年份(年份访问者)

public int getYear(){

返回年份;

} //结束方法


//汽车返回价格(价格访问者)

public int getPrice(){

返回价格;

} //结束方法


//打印方式

public void print(){

System.out.println(" Make =" + getMake()+"," +" Model =" + getModel()+ "," +年= + getYear()+"," +价格= $ + getPrice());

} //结束方法

}


即用于创建新的Car.java文件汽车


/ * UMUC - CMIS 141 - 项目4

*文件:CarDealerApp.java

*作者:Annie Ideus- Balboa

*日期:2007年5月29日

*使用指示:从命令窗口运行程序

* /



import java.util。*;

import java.net。*;

import java.lang。*;


公共类CarDealerApp {



public static void main(String [] args){


//声明Car对象的数组。数组名称是carShop

final int MAX_SIZE = 100;

Car carShop [] =新车[MAX_SIZE];


扫描仪stdin = new Scanner(System.in);


//自我介绍

System.out.println();

System.out.println(" CMIS 141项目3);

System.out.println(" File:Car.java");

System.out.println("创建者:Annie( ALI)Ideus-Balboa");

System.out.println(" Due:June 3,2007");

System.out.println();


System.out.print(请输入当前店内有效车辆的数量?:);

int numberOfCars = stdin。 nextInt();


for(int i = 0;(i< numberOfCars)&&(i< MAX_SIZE); ++ i){
< br $>
System.out.println();

System.out.println(" Your Car(Car c3):");

System.out.print(你车的品牌是什么?);

String m = stdin.nextLine();

System.out.print("你车的型号是什么?);

String d = stdin.nextLine();

System.out.print("你车的年份是多少?);

int y = stdin.nextInt();

System.out.print("你的车的价格是多少?$");

int p = stdin.nextInt ();

Car C =新车(m,d,y,p);

carShop [i] = C;

}

for(int i = 0;(i< numberOfCars)&&(i< MAX_SIZE); ++ i){

System.out.println (carShop [i]);

}

}

}


现在我跑这个打印出来了

车@ 14318bb

车@ ca0b6


有谁可以帮我这个?在此先感谢ALI:P

here is the instructiions I am to use

Ask the user to input the number of effective cars in the shop (this number should be between 1 and 100, inclusively). In a loop, controlled by the inputted number of effective cars, ask the user to input each car data (i.e. make, model, year and price). You can assume that all input data is correct and no data validation is necessary. As soon as all input data for a car is entered, build a Car object by the constructor with parameters and then save the Car object in the array.

Here is what i have so far:

/* UMUC - CMIS 141 - Project 4
* File: Car.java
* Author: Annie Ideus-Balboa
* Date: May 13th, 2007
* Complete: May 17th, 2007
* Modified for Project 4: May 29th, 2007
* Use indications: Run the program from a command window
*/


import java.util.*;
import java.net.*;
import java.lang.*;

public class Car{

public static String make;
public static String model;
public static int year;
public static int price;


//Car(); constructor with parameters
public Car(String m, String d, int y, int p) {
make = m;
model = d;
year = y;
price = p;
}//end constructor

// return make of car (make accessor)
public String getMake() {
return make;
}//end method

//return model of car (model accessor)
public String getModel() {
return model;
}//end method

//return year of car (year accessor)
public int getYear() {
return year;
}//end method

//return price of car (price accessor)
public int getPrice() {
return price;
}//end method

//print method
public void print() {
System.out.println("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
}//end method
}

that is Car.java file used to create new Cars

/* UMUC - CMIS 141 - Project 4
* File: CarDealerApp.java
* Author: Annie Ideus-Balboa
* Date: May 29thth, 2007
* Use indications: Run the program from a command window
*/


import java.util.*;
import java.net.*;
import java.lang.*;

public class CarDealerApp{



public static void main(String[] args) {

//declare array of Car objects. Array name is carShop
final int MAX_SIZE = 100;
Car carShop[] = new Car[MAX_SIZE];

Scanner stdin = new Scanner(System.in);

//self intro
System.out.println();
System.out.println(" CMIS 141 Project 3");
System.out.println(" File: Car.java");
System.out.println(" Created by: Annie (ALI) Ideus-Balboa");
System.out.println(" Due: June 3rd, 2007");
System.out.println();

System.out.print("Please input the number of effective cars currently in the shop?: ");
int numberOfCars = stdin.nextInt();

for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {

System.out.println();
System.out.println("Your Car (Car c3):");
System.out.print("What is the make of your car? ");
String m = stdin.nextLine();
System.out.print("What is the model of your car? ");
String d = stdin.nextLine();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("What is the Price of your car? $");
int p = stdin.nextInt();
Car C = new Car(m, d, y, p);
carShop[i] = C;
}
for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
System.out.println(carShop[i]);
}
}
}

now when I run this the print comes out as
Car@14318bb
Car@ca0b6

Can anyone help me with this? Thanks in Advance ALI :P

推荐答案

" + getPrice());

} //结束方法

}


即用于创建新的Car.java文件汽车


/ * UMUC - CMIS 141 - 项目4

*文件:CarDealerApp.java

*作者:Annie Ideus- Balboa

*日期:2007年5月29日

*使用指示:从命令窗口运行程序

* /



import java.util。*;

import java.net。*;

import java.lang。*;


公共类CarDealerApp {



public static void main(String [] args){


//声明Car对象的数组。数组名称是carShop

final int MAX_SIZE = 100;

Car carShop [] =新车[MAX_SIZE];


扫描仪stdin = new Scanner(System.in);


//自我介绍

System.out.println();

System.out.println(" CMIS 141项目3);

System.out.println(" File:Car.java");

System.out.println("创建者:Annie( ALI)Ideus-Balboa");

System.out.println(" Due:June 3,2007");

System.out.println();


System.out.print(请输入当前店内有效车辆的数量?:);

int numberOfCars = stdin。 nextInt();


for(int i = 0;(i< numberOfCars)&&(i< MAX_SIZE); ++ i){
< br $>
System.out.println();

System.out.println(" Your Car(Car c3):");

System.out.print(你车的品牌是什么?);

String m = stdin.nextLine();

System.out.print("你车的型号是什么?);

String d = stdin.nextLine();

System.out.print("你车的年份是多少?);

int y = stdin.nextInt();

System.out.print("你的车的价格是多少?
" + getPrice());
}//end method
}

that is Car.java file used to create new Cars

/* UMUC - CMIS 141 - Project 4
* File: CarDealerApp.java
* Author: Annie Ideus-Balboa
* Date: May 29thth, 2007
* Use indications: Run the program from a command window
*/


import java.util.*;
import java.net.*;
import java.lang.*;

public class CarDealerApp{



public static void main(String[] args) {

//declare array of Car objects. Array name is carShop
final int MAX_SIZE = 100;
Car carShop[] = new Car[MAX_SIZE];

Scanner stdin = new Scanner(System.in);

//self intro
System.out.println();
System.out.println(" CMIS 141 Project 3");
System.out.println(" File: Car.java");
System.out.println(" Created by: Annie (ALI) Ideus-Balboa");
System.out.println(" Due: June 3rd, 2007");
System.out.println();

System.out.print("Please input the number of effective cars currently in the shop?: ");
int numberOfCars = stdin.nextInt();

for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {

System.out.println();
System.out.println("Your Car (Car c3):");
System.out.print("What is the make of your car? ");
String m = stdin.nextLine();
System.out.print("What is the model of your car? ");
String d = stdin.nextLine();
System.out.print("What is the Year of your car? ");
int y = stdin.nextInt();
System.out.print("What is the Price of your car?


");

int p = stdin.nextInt();

Car C = new Car(m,d,y,p);

carShop [i] = C ;

}

for(int i = 0;(i< numberOfCars)&&(i< MAX_SIZE); ++ i){

System.out.println(carShop [i]);

}

}

}


现在当我运行这个打印出来时

车@ 14318bb

车@ ca0b6


谁能帮我这个?在此先感谢ALI:P
");
int p = stdin.nextInt();
Car C = new Car(m, d, y, p);
carShop[i] = C;
}
for (int i= 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
System.out.println(carShop[i]);
}
}
}

now when I run this the print comes out as
Car@14318bb
Car@ca0b6

Can anyone help me with this? Thanks in Advance ALI :P



这里是我要使用的指示


问用户输入商店中有效车辆的数量(此数字应介于1和100之间,包括在内)。在由输入的有效车辆数量控制的循环中,要求用户输入每个车辆数据(即品牌,型号,年份和价格)。您可以假设所有输入数据都是正确的,无需进行数据验证。输入汽车的所有输入数据后,构造函数使用参数构建Car对象,然后将Car对象保存在数组中。


这是我到目前为止所拥有的:


/ * UMUC - CMIS 141 - 项目4

*文件:Car.java

*作者:Annie Ideus- Balboa

*日期:2007年5月13日

*完成时间:2007年5月17日

*修改为项目4:2007年5月29日

*使用指示:从命令窗口运行程序

* /


import java.util。* ;

导入java.net。*;

导入java.lang。*;


公共类汽车{


public static String make;

public static String model;

public static int year;

public static int price;


// Car();带参数的构造函数

public Car(String m,String d,int y,int p){

make = m;

model = d ;

年= y;

price = p;

} //结束构造函数


//返回make of make(make accessor)

public String getMake(){

return make;

} // end method


//返回汽车模型(模型访问者)

public String getModel(){

返回模型;

} //结束方法


//返回汽车年份(年份访问者)

public int getYear(){

返回年份;

} //结束方法


//汽车返回价格(价格访问者)

public int getPrice(){

返回价格;

} //结束方法


//打印方式

public void print(){

System.out.println(" Make =" + getMake()+"," +" Model =" + getModel()+ "," +" Year =" + getYear() +, +价格=
here is the instructiions I am to use

Ask the user to input the number of effective cars in the shop (this number should be between 1 and 100, inclusively). In a loop, controlled by the inputted number of effective cars, ask the user to input each car data (i.e. make, model, year and price). You can assume that all input data is correct and no data validation is necessary. As soon as all input data for a car is entered, build a Car object by the constructor with parameters and then save the Car object in the array.

Here is what i have so far:

/* UMUC - CMIS 141 - Project 4
* File: Car.java
* Author: Annie Ideus-Balboa
* Date: May 13th, 2007
* Complete: May 17th, 2007
* Modified for Project 4: May 29th, 2007
* Use indications: Run the program from a command window
*/


import java.util.*;
import java.net.*;
import java.lang.*;

public class Car{

public static String make;
public static String model;
public static int year;
public static int price;


//Car(); constructor with parameters
public Car(String m, String d, int y, int p) {
make = m;
model = d;
year = y;
price = p;
}//end constructor

// return make of car (make accessor)
public String getMake() {
return make;
}//end method

//return model of car (model accessor)
public String getModel() {
return model;
}//end method

//return year of car (year accessor)
public int getYear() {
return year;
}//end method

//return price of car (price accessor)
public int getPrice() {
return price;
}//end method

//print method
public void print() {
System.out.println("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price =


这篇关于将对象添加到数组时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆