我如何将所有变量串在一起以便用一个句子表达它们?

How do I string all the variables together in order to articulate them in one sentence?

当前代码单独打印语句,而我想将所有关于汽车的变量(颜色、气缸数和座位数)串在一起,并将所有关于卡车的变量(颜色、数量)串在一起气瓶和牵引能力)。可变座位在子类 Car 中并打印其自己的独立语句,变量牵引能力在子类 Truck 中也打印其自己的语句。我想要汽车的所有变量都在一个句子中,卡车的所有变量都在一个句子中。

import java.util.Scanner;

class Vehicle {

    Scanner s = new Scanner(System.in);
    private String color;
    private int noOfCylinders;
    public int noOfSeats;
    public int towingCapacity;

    public Vehicle() {
        color = "Black";
        noOfCylinders = 0;
        noOfSeats = 1;
        towingCapacity =0;
    }

    public Vehicle(String color, int noOfCylinders, int noOfSeats, int towingCapacity) {
        this.color = color;
        this.noOfCylinders = noOfCylinders;
        this.noOfSeats = noOfSeats;
        this.towingCapacity = towingCapacity;
    }

    public void setColor() {
        System.out.print("Enter color of vehicle: ");
        color = s.nextLine();
    }

    public String getColor() {
        return color;
    }

    public void setNoOfCylinders() {
        System.out.print("Enter number of cylinders: ");
        noOfCylinders = s.nextInt();
    }

    public int getNoOfCylinders() {
        return noOfCylinders;
    }

    public void setNoOfSeats() {
        System.out.print("Enter numer of seats: ");
        noOfSeats = s.nextInt();
    }

    public void setTowingCapacity() {
        System.out.print("Enter towing Capacity: ");
        towingCapacity = s.nextInt();
    }

    public String toString() {
        String information;
        information = "is " + color + " and it has " + noOfCylinders + " cylinders.";
        return information;
    }
}

public class CreateVehicle {
    public static void main(String[] args) {

        Car CarObject = new Car();
        Truck TruckObject = new Truck();

        CarObject.getColor();
        CarObject.setColor();
        CarObject.getNoOfCylinders();
        CarObject.setNoOfCylinders();
        CarObject.toString();
        CarObject.setNoOfSeats();

        TruckObject.getColor();
        TruckObject.setColor();
        TruckObject.getNoOfCylinders();
        TruckObject.setNoOfCylinders();
        TruckObject.toString();
        TruckObject.setTowingCapacity();

        CarObject.toStringSeats();
        TruckObject.toStringCapacity();
        System.out.print(("\nThe car ")+CarObject.toString());
        System.out.print(("\nThe truck ")+TruckObject.toString());      
    }
}


class Car extends Vehicle{
    public void toStringSeats(){
        System.out.print("\nThe car has " + noOfSeats + " seats.");
    }
}

    class Truck extends Vehicle {
        public void toStringCapacity() {
            System.out.print("\nThe truck has a towing capacity of " + towingCapacity + ".");
        }
    }

你不需要一些代码,因为我在下面评论了相同的内容。 使用下面给出的 toString 方法。

public class CreateVehicle {
    public static void main(String[] args) {

        Car CarObject = new Car();
        Truck TruckObject = new Truck();

        //CarObject.getColor();
        CarObject.setColor();
        //  CarObject.getNoOfCylinders();
        CarObject.setNoOfCylinders();
        //CarObject.toString();
        CarObject.setNoOfSeats();

        //TruckObject.getColor();
        TruckObject.setColor();
        //TruckObject.getNoOfCylinders();
        TruckObject.setNoOfCylinders();
        //TruckObject.toString();
        TruckObject.setTowingCapacity();

        System.out.print(("\nThe car ") + CarObject.toString());
        System.out.print(("\nThe truck ") + TruckObject.toString());
    }
}

    class Car extends Vehicle {
        public String toString() {
            String information;
            information = super.toString() + " and " + noOfSeats + " seats";
            return information;
        }
    }

    class Truck extends Vehicle {
        public String toString() {
            String information;
            information = super.toString() + " and has a towing capacity of " + 
            towingCapacity;
            return information;
        }
    }