gpt4 book ai didi

java - 自从我添加了 super 关键字后,代码就没有输出任何内容

转载 作者:行者123 更新时间:2023-12-02 02:48:32 24 4
gpt4 key购买 nike

使用继承概念,我在卡车和汽车类之间有共同的变量,例如气缸数量和颜色,最重要的是,卡车类有一个用于牵引能力的变量,而汽车类有一个用于数量的变量座位。因此,我决定使用关键字super来引用父类,但似乎没有输出。

import java.util.Scanner;
class Vehicle {
String color;
int noOfCylinders;
public Vehicle(String color, int noOfCylinders) {
this.color = "Black";
this.noOfCylinders = 0;
}
public void setColor(String c) {
color = c;
}
public String getColor() {
return color;
}
public void setNoOfCylinders(int noOfCyl) {
noOfCylinders = noOfCyl;
}
public int getNoOfCylinders() {
return noOfCylinders;
}
public String toString() {
String information;
information = "is " + color + " and it has " + noOfCylinders + " cylinders";
return information;
}
}
public class CreateVehicle {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Car CarObject = new Car(s.nextLine(), s.nextInt(), s.nextInt());
Truck TruckObject = new Truck(s.nextLine(), s.nextInt(), s.nextInt());
System.out.print("Enter the color of the car: ");
CarObject.setColor(s.nextLine());
System.out.print("Enter the number of cylinders in the car: ");
CarObject.setNoOfCylinders(s.nextInt());

System.out.print("Enter the number of seats in the car: ");
CarObject.setNoOfSeats(s.nextInt());
s.nextLine();
System.out.print("\nEnter the color of the truck: ");
TruckObject.setColor(s.nextLine());
System.out.print("Enter the number of cylinders in the truck: ");
TruckObject.setNoOfCylinders(s.nextInt());

System.out.print("Enter the towing capacity of the truck (lbs): ");
TruckObject.setTowingCapacity(s.nextInt());
System.out.print(("\nThe car ") + CarObject.toString() + ". ");
System.out.print(("\nThe truck ") + TruckObject.toString()+ ". ");
}
}
class Car extends Vehicle {
private int noOfSeats;
public Car(String color, int noOfCylinders, int seatNum) {
super(color, noOfCylinders);
this.noOfSeats = seatNum;
}
public void setNoOfSeats(int noOfSeat) {
noOfSeats = noOfSeat;
}

public String toString() {
System.out.print("The car has " + noOfSeats + " seats.");

return super.toString();
}
}
class Truck extends Vehicle {
private int towingCapacity;
public Truck(String color, int noOfCylinders, int capacityTowing) {
super(color, noOfCylinders);
this.towingCapacity = capacityTowing;
}

public void setTowingCapacity(int towingCapacityTruck) {
towingCapacity = towingCapacityTruck;
}

public String toString() {
System.out.print ("The truck has a towing capacity of " + towingCapacity + " lbs.");
return super.toString();
}
}

*

最佳答案

    package problems;

import java.util.Scanner;

public class CreateVehicle {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Car CarObject = new Car(s.next(), s.nextInt(), s.nextInt());
Truck TruckObject = new Truck(s.next(), s.nextInt(), s.nextInt());
System.out.print("Enter the color of the car: ");
CarObject.setColor(s.next());
System.out.print("Enter the number of cylinders in the car: ");
CarObject.setNoOfCylinders(s.nextInt());

System.out.print("Enter the number of seats in the car: ");
CarObject.setNoOfSeats(s.nextInt());
s.nextLine();
System.out.print("\nEnter the color of the truck: ");
TruckObject.setColor(s.next());
System.out.print("Enter the number of cylinders in the truck: ");
TruckObject.setNoOfCylinders(s.nextInt());

System.out.print("Enter the towing capacity of the truck (lbs): ");
TruckObject.setTowingCapacity(s.nextInt());
System.out.print(("\nThe car ") + CarObject.toString() + ". ");
System.out.print(("\nThe truck ") + TruckObject.toString() + ". ");
}
}

class Truck extends Vehicle {
private int towingCapacity;
public Truck(String color, int noOfCylinders, int capacityTowing) {
super(color, noOfCylinders);
this.towingCapacity = capacityTowing;
}

public void setTowingCapacity(int towingCapacityTruck) {
towingCapacity = towingCapacityTruck;
}

public String toString() {
System.out.print ("The truck has a towing capacity of " + towingCapacity + " lbs.");
return super.toString();
}
}


class Car extends Vehicle {
private int noOfSeats;
public Car(String color, int noOfCylinders, int seatNum) {
super(color, noOfCylinders);
this.noOfSeats = seatNum;
}
public void setNoOfSeats(int noOfSeat) {
noOfSeats = noOfSeat;
}

public String toString() {
System.out.print("The car has " + noOfSeats + " seats.");

return super.toString();
}
}

class Vehicle {
String color;
int noOfCylinders;
public Vehicle(String color, int noOfCylinders) {
this.color = "Black";
this.noOfCylinders = 0;
}
public void setColor(String c) {
color = c;
}
public String getColor() {
return color;
}
public void setNoOfCylinders(int noOfCyl) {
noOfCylinders = noOfCyl;
}
public int getNoOfCylinders() {
return noOfCylinders;
}
public String toString() {
String information;
information = "is " + color + " and it has " + noOfCylinders + " cylinders";
return information;
}
}

我刚刚将 nextLine 更改为 next,假设您只期望一个单词作为输入,它是一个颜色字符串。结果成功了。

关于java - 自从我添加了 super 关键字后,代码就没有输出任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57126432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com