小さな車のディーラープログラムがありますが、追加された車やバイクを表示することを選択するまでは、すべてが正しく動作しているようです(メニュー、arraylistには車とバイクを追加します) 。サブクラスで実行している表示方法は実行されますが、入力した値は表示されません。モデル=ヌル、価格= 0.0、燃料タイプ=ヌルなどarraylistを表示するとヌル値が表示されます
誰かがここにいるのは数秒で解決できると確信しています!より多くのコードが必要な場合 はちょうど私はエラーが私はちょうどスーパークラス車とドライバーの入力や表示方法
おかげ
public class Vehicles
{
private final String make = "BMW";
private String model;
private double price;
private String colour;
private int stock;
private double fuelMpg;
private float displacement;
private int topSpeed;
public Vehicles()
{
model = "";
price = 0.0;
colour = "";
stock = 0;
fuelMpg = 0;
displacement = 0;
topSpeed = 0;
}
public Vehicles(String model, double price, String colour, int stock, double fuelMpg, float displacement, int topSpeed)
model = model;
price = price;
colour = colour;
stock = stock;
fuelMpg = fuelMpg;
displacement = displacement;
topSpeed = topSpeed;
}
public void display()
{
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Price: " + price);
System.out.println("Colour: " + colour);
System.out.println("fuelMpg: " + fuelMpg);
System.out.println("displacement: " + displacement);
System.out.println("topSpeed: " + topSpeed);
}
public void inputCarDetails()
{
Scanner scan = new Scanner(System.in);
String model, colour, fuelType, frame;
int doors, stock, topSpeed, stroke, noSeats, noVehicles, noCar;
float displacement;
double price, fuelMpg;
boolean sunroof;
Vehicles car;
System.out.println("Enter the amount of cars you want to add to the brochure");
noCar = scan.nextInt(); scan.nextLine();
for (int i = 0; i < noCar; i++) {
System.out.println("----Entering car details----");
System.out.println("\nEnter model");
model = scan.nextLine();
System.out.println("Enter Price");
price = scan.nextDouble(); scan.nextLine();
System.out.println("Enter colour");
colour = scan.nextLine();
System.out.println("Enter no. in stock");
stock = scan.nextInt(); scan.nextLine();
System.out.println("Enter MPG");
fuelMpg = scan.nextDouble(); scan.nextLine();
System.out.println("Enter displacement");
displacement = scan.nextInt(); scan.nextLine();
System.out.println("Enter top speed");
topSpeed = scan.nextInt(); scan.nextLine();
System.out.println("Enter no. of doors");
doors = scan.nextInt(); scan.nextLine();
System.out.println("Enter fuel type");
fuelType = scan.nextLine();
System.out.println("Enter sunroof (true/false)");
sunroof = scan.nextBoolean(); scan.nextLine();
car = new Cars(model, price, colour, stock, fuelMpg, displacement, topSpeed, doors, fuelType, sunroof);
list.add(car);
}
}
public void inputBikeDetails()
{
Scanner scan = new Scanner(System.in);
String model, colour, fuelType, frame;
int doors, stock, displacement, topSpeed, stroke, noSeats, noVehicles, noBike;
double price, fuelMpg;
boolean sunroof;
Vehicles bike;
System.out.println("Enter the amount of bikes you want to add to the brochure");
noBike = scan.nextInt(); scan.nextLine();
for (int i = 0; i < noBike; i++) {
System.out.println("----Entering bike details----");
System.out.println("\nEnter model");
model = scan.nextLine();
System.out.println("Enter Price");
price = scan.nextDouble(); scan.nextLine();
System.out.println("Enter colour");
colour = scan.nextLine();
System.out.println("Enter no. in stock");
stock = scan.nextInt(); scan.nextLine();
System.out.println("Enter MPG");
fuelMpg = scan.nextDouble(); scan.nextLine();
System.out.println("Enter displacement");
displacement = scan.nextInt(); scan.nextLine();
System.out.println("Enter top speed");
topSpeed = scan.nextInt(); scan.nextLine();
System.out.println("Enter engine stroke");
stroke = scan.nextInt(); scan.nextLine();
System.out.println("Enter no. of seats");
noSeats = scan.nextInt(); scan.nextLine();
System.out.print("Enter the frame type");
frame = scan.nextLine();
bike = new Bikes(model, price, colour, stock, fuelMpg, displacement, topSpeed, stroke, noSeats, frame);
list.add(bike);
}
}
public void displayCars()
{
if (list.isEmpty()) {
System.out.println("Unfortunatly, we have no cars on sale at the moment");
}
else {
System.out.println("\n****Car Brochure****");
for (Vehicles v : list)
if (v instanceof Cars) {
v.display();
}
}
}
public void displayBikes()
{
if (list.isEmpty()) {
System.out.println("Unfortunatly, we have no motorbikes on sale at the moment");
}
else {
System.out.println("\n****Motorbike Brochure****");
for (Vehicles v : list)
if (v instanceof Bikes) {
v.display();
}
}
}
public static void main (String[] args) // main method
{
BMWdriver driver = new BMWdriver();
driver.Driver();
driver.startMenu();
driver.inputCarDetails();
driver.inputBikeDetails();
}
public class Cars extends Vehicles
{
private int doors;
private String fuelType;
private final String layout = "RWD";
private boolean sunroof;
public Cars()
{
super();
doors = 0;
fuelType = "";
sunroof = false;
}
public Cars(String model, double price, String colour, int stock, double fuelMpg, float displacement, int topSpeed, int doors, String fuelType, boolean sunroof)
{
super(model, price, colour, stock, fuelMpg, displacement, topSpeed);
doors = doors;
fuelType = fuelType;
sunroof = sunroof;
}
public void display()
{
super.display();
System.out.println("No. of doors: " + doors);
System.out.println("Fuel: " + fuelType);
System.out.println("Wheel layout: " + layout);
if (sunroof = false) {
System.out.println("This car has no sunroof");
}
else {
System.out.println("This car has a sunroof");
}
}
あなたは私たちが 'Car'クラスを示していただけますか? –
これはいわゆるリストです – 3kings