【问题标题】:Main prints nulls and 0; instead of the correct names; due to array/list issue主打印空值和 0;而不是正确的名称;由于数组/列表问题
【发布时间】:2019-07-04 12:06:34
【问题描述】:
Last Block of main prints:
null, 0, null 
null, 0, null
null, 0, null
null, 0, null
But should print the following: Ferrari 488, 0, Ferrari; Audi A7, 0, Audi
Ferrari F1, 20, Ferrari; AudiR8, 3, Audi

尝试将数组列表类型更改为字符串,这被提及为错误。 尝试了几种列表类型。但我没有得到指定的输出,只有 0/null 的输出,我一直在尝试很多东西;希望任何人都可以提供帮助。

import java.util.ArrayList;

class Car {
    String name;
    String type;
    int age;

    Car() {
        name = null;
        type = null;
        age = 0;
    }

    Car(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age = age;
    }

    // GETTERS:
    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public int getAge() {
        return age;
    }

    // SETTER:
    public void setAge(int age) {
        this.age = age;
    }

    public void printInfo() {
        System.out.println(name + ", " + age + ", " + type);
    }
}
class Audi extends Car {
    String type = "Audi";

    Audi() {
        super("Audi A7", "Audi", 0);
    }

    Audi(String name, int age) {
        super(name, "Audi", age);
    }
}
class Ferrari extends Car {
    String type = "Ferrari";

    Ferrari() {
        super("Ferrari 488", "Ferrari", 0);
    }

    Ferrari(String name, int age) {
        super(name, "Ferrari", age);
    }
}
class Mercedes extends Car {
    String type = "Mercedes";

    Mercedes() {
        super("AMGGT", "Mercedes", 0);
    }

    Mercedes(String name, int age) {
        super(name, "Mercedes", age);
    }
}
class Dealer {
    // ArrayList for Cars
    private ArrayList<Car> cars = new ArrayList<>();

    public void addCar(Car a) {
        cars.add(a);
    }

    public void printAllInfo() {

        Car print = new Car();
        print.printInfo();

        for (Car a : cars) {
            print.printInfo();
        }
    }
}
public class CarBuilder {
    public static void main(String[] args) {
        Ferrari e1 = null, e2 = null;
        Audi p1 = null, p2 = null;
        Dealer z = null;

        try {
            e1 = new Ferrari();
            if (e1.name.equals("Ferrari 488")) {
                System.out.println("PASS1");
            } else {
                System.out.println("FAIL1");
            }
            if (e1.type.equals("Ferrari")) {
                System.out.println("PASS2");
            } else {
                System.out.println("FAIL2");
            }
            if (e1.age == 0) {
                System.out.println("PASS3");
            } else {
                System.out.println("FAIL3");
            }
        } catch (Exception e) {
            System.out.println("FAIL-block1");
        }

        try {
            p1 = new Audi();
            if (p1.name.equals("Audi A7")) {
                System.out.println("PASS4");
            } else {
                System.out.println("FAIL4");
            }
            if (p1.type.equals("Audi")) {
                System.out.println("PASS5");
            } else {
                System.out.println("FAIL5");
            }
            if (p1.age == 0) {
                System.out.println("PASS6");
            } else {
                System.out.println("FAIL6");
            }
        } catch (Exception e) {
            System.out.println("FAIL-block2");
        }

        try {
            e2 = new Ferrari("Ferrari F1", 20);
            if (e2.name.equals("Ferrari F1")) {
                System.out.println("PASS7");
            } else {
                System.out.println("FAIL7");
            }
            if (e2.type.equals("Ferrari")) {
                System.out.println("PASS8");
            } else {
                System.out.println("FAIL8");
            }
            if (e2.age == 20) {
                System.out.println("PASS9");
            } else {
                System.out.println("FAIL9");
            }
        } catch (Exception e) {
            System.out.println("FAIL-block3");
        }

        try {
            p2 = new Audi("AudiR8", 3);
            if (p2.name.equals("AudiR8")) {
                System.out.println("PASS10");
            } else {
                System.out.println("FAIL10");
            }
            if (p2.type.equals("Audi")) {
                System.out.println("PASS11");
            } else {
                System.out.println("FAIL11");
            }
            if (p2.age == 3) {
                System.out.println("PASS12");
            } else {
                System.out.println("FAIL12");
            }
        } catch (Exception e) {
            System.out.println("FAIL-block4");
        }

        System.out.println("--- Testing printInfo function ---");
        try {
            e1.printInfo();
        } catch (Exception e) {
            System.out.println("FAIL-print1");
        }
        try {
            p1.printInfo();
        } catch (Exception e) {
            System.out.println("FAIL-print2");
        }
        try {
            e2.printInfo();
        } catch (Exception e) {
            System.out.println("FAIL-print3");
        }
        try {
            p2.printInfo();
        } catch (Exception e) {
            System.out.println("FAIL-print4");
        }

        System.out.println("--- Testing printAll function. Should match the above output ---");
        try {
            z = new Dealer();
            z.addCar((Car) e1);
            z.addCar((Car) p1);
            z.addCar((Car) e2);
            z.addCar((Car) p2);
            z.printAllInfo();
        } catch (Exception e) {
            System.out.println("FAIL-lastblock1");
            System.out.println("FAIL-lastblock2");
            System.out.println("FAIL-lastblock3");
            System.out.println("FAIL-lastblock4");
        }
    }
}

应该打印:法拉利 488、0、法拉利 而不是 null, 0, null

【问题讨论】:

  • 您希望我们花时间了解您的问题并提供答案,但首先您应该花时间发布格式良好的代码。为什么让你的代码比必要的更难阅读和理解?
  • 代码实际上是两个文件 CarBuilder(从 Carbuilder 类开始)和 Dealer。
  • 再次,请好好格式化。
  • 很抱歉;明白这是很多代码,我试图缩短它,但我认为它会使事情变得非常复杂。除了 main 中最后一个块的输出之外,代码都可以工作。
  • 不是长度,而是格式。这次我已经为你格式化了你的代码,但这又是你的问题,所以在未来,做这件事的努力应该是你的(如果你想要一个体面的答案)。

标签: java arrays list printing


【解决方案1】:

您的经销商类代码仅打印空车:

public void printAllInfo() {

    Car print = new Car();  // null-filled Car
    print.printInfo();

    for (Car a : cars) {
        print.printInfo();  // !!!!! HERE !!!!
    }
}

在指示的行中,您打印的不是a 的数据,而是空填充的打印数据。

相反,它应该是:

public void printAllInfo() {

    // ** No real need for these lines:
    // Car print = new Car();
    // print.printInfo();

    for (Car a : cars) { 
        // print.printInfo();
        a.printInfo();   // *** note the change? ***
    }
}

【讨论】:

  • 满载鳗鱼的气垫船;谢谢你!!!!!我在错误的区域寻找了几个小时,但无法弄清楚。用新代码编译它,它可以工作。非常感谢!
  • @PBody123:不客气。一个有用的参考是How to debug small programs,你会想要完整地阅读它。
  • 感谢您的帮助和有用的信息。非常高兴您的帮助充满鳗鱼的气垫船!
猜你喜欢
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多