Java 从arraylist中获取价值(通过调用computeCars()获得所有三辆车的总成本)
正在尝试从ArrayList中的中获取值。这是我的代码示例Java 从arraylist中获取价值(通过调用computeCars()获得所有三辆车的总成本),java,Java,正在尝试从ArrayList中的中获取值。这是我的代码示例 public static void main (String [] args){ Car toyota= new Car("Toyota", "$10000", "300"+ "2003"); Car nissan= new Car("Nissan", "$22000", "300"+ "2011"); Car ford= new Car("Ford", "$15000", "350"+ "2010");
public static void main (String [] args){
Car toyota= new Car("Toyota", "$10000", "300"+ "2003");
Car nissan= new Car("Nissan", "$22000", "300"+ "2011");
Car ford= new Car("Ford", "$15000", "350"+ "2010");
ArrayList<Car> cars = new ArrayList<Car>();
cars.add(toyota);
cars.add(nissan);
cars.add(ford);
}
public static void processCar(ArrayList<Car> cars){
// in heare i need a way of getting the total cost of all three cars by calling
// computeCars ()
System.out.println(cars.get());
}
在班上
public static void processCar(ArrayList<Car> cars){
int totalAmount=0;
for (int i=0; i<cars.size(); i++){
cars.get(i).computeCars ();
totalAmount=+ ?? // in need to add the computed values of totalprice from the Car class?
}
}
publicstaticvoidprocesscar(arraylistcars){
int totalAmount=0;
对于(int i=0;i
你必须改变你的方法,比如:
public int computeCars (){
int totalprice= price+tax;
System.out.println (name + "\t" +totalprice+"\t"+year );
return totalprice;
}
你在这里也犯了错误:
Car toyota= new Car("Toyota", "$10000", "300"+ "2003");
您应该更改创建汽车对象的样式:
Car toyota= new Car("Toyota", 10000, 300, 2003);
将computeCars()
的类型更改为int
,调用该方法时返回totalprice
class Car {
public Car (String name, int price, int, tax, int year){
constructor.......
}
public int computeCars (){
int totalprice= price+tax;
System.out.println (name + "\t" +totalprice+"\t"+year );
return totalprice;
}
}
然后在for
循环中,将totalprice
添加到totalamount
中,针对每辆汽车
public static void processCar(ArrayList<Car> cars){
int totalAmount=0;
for (int i=0; i<cars.size(); i++){
totalAmount+= cars.get(i).computeCars(); // compute totalprice for each car
}
System.out.println(totalAmount);
}
在进行所有更改之后,您可能会出现大量的编译错误
,因为来自main方法的构造函数
调用与定义的构造函数
不匹配
因此,您需要在构造函数的调用中进行更改
Car toyota= new Car("Toyota", 10000, 300, 2003);
Car nissan= new Car("Nissan", 22000, 300, 2011);
Car ford= new Car("Ford", 15000, 350, 2010);
我认为您可以让computeCars()
方法返回值,然后将for
循环的第一条指令移动为用于增加总数的值
class Car {
public Car (String name, int price, int, tax, int year){
constructor.......
}
public int computeCars(){
return totalprice= price+tax;
}
public void printComputeCars(){
System.out.println (name + "\t" + computeCars() +"\t" + year );
}
}
public static void processCar(ArrayList<Car> cars){
int totalAmount=0;
for (int i=0; i<cars.size(); i++){
totalAmount=+ cars.get(i).computeCars();
}
}
等级车{
公共汽车(字符串名称、整数价格、整数、税、整数年){
建造商。。。。。。。
}
公共int计算车(){
退货总价=价格+税金;
}
公共车辆{
System.out.println(名称+“\t”+计算车()+“\t”+年份);
}
}
公共静态车辆(ArrayList车辆){
int totalAmount=0;
对于(int i=0;i在Car类中添加一个方法来计算总价:
public int getTotalPrice() {
return price + tax;
}
public void computeCars (){
System.out.println (name + "\t" +getTotalPrice()+"\t"+year );
}
然后您的processCars方法变成:
public static void processCar(ArrayList<Car> cars){
int totalAmount=0;
for (int i=0; i<cars.size(); i++){
totalAmount=+ cars.get(i).getTotalPrice();
}
System.out.println("Total: " + totalAmount);
}
publicstaticvoidprocesscar(arraylistcars){
int totalAmount=0;
对于(int i=0;i如何从computeCars()返回价格)
?你的Car
构造函数和你如何调用它…这不在一起。签名是String,int,int,int
,但是你传入了3个String
s-这可能根本没有编译,是吗?计算车应该返回一个值。@domdom这不是我们能看到的唯一错误,是谁给了一个d喜欢这样的问题吗?同一课程的同学,谁和你一起做作业?@domdom我有时喜欢这些问题,以便以后更容易找到,希望它们能被编辑。也许会提到这只是Java 8+(或者我弄错了?)此外,OP的代码还有很多错误。是的,是的,但简而言之,这是一个解决方案:)另一个是编译错误,我想他/她无论如何都会解决。这对所有其他答案都是正确的,但你的答案目前投票率最高,所以我在这里要指出:我希望有人也能指出OPs代码中的其他问题,包括糟糕的设计-computeCars()例如,
,对于该方法的功能而言,这是一个可怕的误导性名称。构造函数以及OP如何调用它?@domdom您还需要我做什么更改吗?Sanket,当然您可以随心所欲。我个人会指出OP如何调用Car
构造函数的问题(String
vsint
,包括使用字符串连接的$
)。
class Car {
public Car (String name, int price, int, tax, int year){
constructor.......
}
public int computeCars(){
return totalprice= price+tax;
}
public void printComputeCars(){
System.out.println (name + "\t" + computeCars() +"\t" + year );
}
}
public static void processCar(ArrayList<Car> cars){
int totalAmount=0;
for (int i=0; i<cars.size(); i++){
totalAmount=+ cars.get(i).computeCars();
}
}
public int getTotalPrice() {
return price + tax;
}
public void computeCars (){
System.out.println (name + "\t" +getTotalPrice()+"\t"+year );
}
public static void processCar(ArrayList<Car> cars){
int totalAmount=0;
for (int i=0; i<cars.size(); i++){
totalAmount=+ cars.get(i).getTotalPrice();
}
System.out.println("Total: " + totalAmount);
}