Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的代码无法输出汽车大小和总费用,我正试图找出问题所在_Java_Inheritance - Fatal编程技术网

Java 我的代码无法输出汽车大小和总费用,我正试图找出问题所在

Java 我的代码无法输出汽车大小和总费用,我正试图找出问题所在,java,inheritance,Java,Inheritance,我有一个任务,我完成了我的代码,但问题是,当我运行它时,车的类型和总费用在最后是空白的,这与继承有关,这对我来说是新的东西,我希望我做对了。 该课程分为3个独立的课程,如有任何帮助,将不胜感激 import java.util.*; public class UseCarRental { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.o

我有一个任务,我完成了我的代码,但问题是,当我运行它时,车的类型和总费用在最后是空白的,这与继承有关,这对我来说是新的东西,我希望我做对了。 该课程分为3个独立的课程,如有任何帮助,将不胜感激

import java.util.*;
public class UseCarRental {
public static void main(String[] args) 
{
       Scanner input = new Scanner(System.in);
        System.out.println("How many days do you need the rental?");
        int rentalLength = input.nextInt();
        System.out.println("Enter requested car size:" +
                "\nEconomy" + "\nMidsize" + "\nFullsize" + "\nLuxury");
        String rentalCarSize = input.nextLine();
        input.next();
        CarRental firstRental = new CarRental(rentalLength, rentalCarSize);
        firstRental.display();
        }

}

class CarRental {
    public  String  rentalCarSize = "";
    public  double rentalFeeDaily;
    public  int rentalLength = 0;
    public  double  rentalFeeTotal= rentalFeeDaily*rentalLength;
    public CarRental(int days, String carSize)
    {
        rentalCarSize   = carSize;
        rentalLength    = days;
    }
    public void display()
    {
        System.out.println(
                "#############" +
                "\nCar Size      = " + getRentalCarSize() +
                "\nRental Length = " + rentalLength +
                "\nTotal Fee     = " + rentalFeeTotal
                );
    }        
    public void setRentalLength(int length)
    {
        rentalLength = length;
    }
    public String getRentalCarSize()
    {
        return rentalCarSize;
    }
    public int getRentalLength()
    {
        return rentalLength;
    }
    public double getRentalFeeTotal()
    {
        return rentalLength * rentalFeeDaily;
    }
    public double getRentalFeeDaily(String carSize)
    {
        switch (carSize)
        {
            case "Economy":
                rentalFeeDaily = 29.99;
                break;
            case "Midsize":
                rentalFeeDaily = 38.99;
                break;
            case "Fullsize":
                rentalFeeDaily = 43.50;
                break;
            case "Luxury":
                rentalFeeDaily = 79.99;
                break;
        }
         return rentalFeeDaily;
        }

    }

import javax.swing.*;
public class LuxuryCarRental extends CarRental
{

 public LuxuryCarRental(int days, String carSize)
    {
        super(days, carSize);
    }

    @Override
    public void display()
    {
        JOptionPane.showMessageDialog(null,
            "\nCar Size      = " + getRentalCarSize() +
            "\nRental Fee    = " + rentalFeeDaily +
            "\nRental Length = " + rentalLength +
            "\nTotal Fee     = " + rentalFeeTotal);
    }
}
在构造函数中设置值后,永远不会计算rentalFeeTotal。将代码更改为:

class CarRental {
    public          String      rentalCarSize    = "";
    public          double      rentalFeeDaily;
    public          int         rentalLength     = 0;
    public          double      rentalFeeTotal   = 0d;

    public CarRental(int days, String carSize)
    {
        rentalCarSize   = carSize;
        rentalLength    = days;
        rentalFeeTotal   = getRentalFeeDaily(carsize)*rentalLength;

    }
您的代码中还有一个问题:

   Scanner input = new Scanner(System.in);
    System.out.println("How many days do you need the rental?");
    int rentalLength = input.nextInt();
    input.nextLine(); // ADD this line
    System.out.println("Enter requested car size:" +
            "\nEconomy" + "\nMidsize" + "\nFullsize" + "\nLuxury");
    String rentalCarSize = input.nextLine();
    input.next();

你试过使用调试器来找出发生了什么吗?@Jens我正在运行Eclipse,它显示的唯一问题是资源泄漏,因为我没有关闭扫描仪。嗨,Jens,谢谢你的快速回复,我将代码更改为你的代码,但它仍然无法计算总费用,我认为这和汽车尺寸的输入有关,但我仍然有问题。@DylanKohel改变了我的答案。谢谢,我自己也发现了另一个问题,那就是我使用了input.next而不是input.nextline