有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

变量的类初始化不起作用(Java类)

需要java作业方面的帮助,我无法编译代码。 我的家庭作业基本上是想让我创建一个名为PetrolPurchase的类来表示您在另一个类中购买的汽油的相关信息。 给出的错误是我的变量可能尚未初始化。 很抱歉粘贴了我的整个文件,但感觉如果我不这样做,上下文将很难理解

import static java.lang.System.*;
import java.util.Scanner;

class PetrolPurchase 
{
    // reader
    Scanner input = new Scanner (in);

    // instance variables
    private String station, type;
    private double quantity, price, payment, discountPrice, netPayment;
    private int discount;

    // constructor
    public PetrolPurchase (String station, String type, 
                           double quantity, double price, 
                           int discount, double payment, 
                           double discountPrice, double netPayment)
                           {
                            this.station = station;
                            this.type = type;
                            this.quantity = quantity;
                            this.price = price;
                            this.discount = discount;
                            this.payment = payment;
                            this.discountPrice = discountPrice;
                            this.netPayment = netPayment;
                           }

    /*public PetrolPurchase (PetrolPurchase duplicate)
    {
        this.station = duplicate.station;
        this.type = duplicate.type;
        this.quantity = duplicate.quantity;
        this.price = duplicate.price;
        this.discount = duplicate.discount;
        this.payment = payment;
        this.discountPrice = discountPrice;
        this.netPayment = netPayment;
    } */

    // accessor methods
    public String getStation ()
    {
        return station;
    }

    public String getType ()
    {
        return type;
    }

    public double getQuantity ()
    {
        return quantity;
    }

    public double getPrice ()
    {
        return price;
    }

    public int getDiscount ()
    {
        return discount;
    }

    // mutator methods
    public void setStation (String station)
    {
        this.station = station;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    public void setQuantity (double quantity)
    {
        this.quantity = quantity;
    }

    public void setPrice (double price)
    {
        this.price = price;
    }

    public void setDiscount (int discount)
    {
        this.discount = discount;
    }

    private double computePayment ()
    {
        payment = price * quantity;
        return payment;
    }

    public double setDiscountPrice ()
    {
        discountPrice = (discount / 100.0) * (price * quantity);
        return discountPrice;
    }

    public double getPayment ()
    {
        netPayment = payment - discountPrice;
        return netPayment;
    }

    // initializing and setting info
    public void setInfo ()
    {
        out.printf ("Enter the station: ");
        station = input.nextLine ();
        setStation (station);
        out.printf ("%nEnter the quantity in liters: ");
        quantity = input.nextDouble ();
        setQuantity (quantity);
        out.printf ("%nEnter type of petrol: ");
        type = input.nextLine ();
        setType (type);
        out.printf ("%nEnter price of petrol: ");
        price = input.nextDouble ();
        setPrice (price);
        out.printf ("%nEnter discount: ");
        discount = input.nextInt ();
        setDiscount (discount);
        out.printf ("%n-----------------------------");
    }

    // displaying all the info
    public void printInfo ()
    {
        out.printf ("%nSummary of your purchase%n");
        out.printf ("-----------------------------%n");
        out.printf ("Station: %s%n", getStation ());
        out.printf ("Total liters: %.2f%n", getQuantity ());
        out.printf ("Petrol type: %s%n", getType ());
        out.printf ("Price per liter: %.2f%n", getPrice ());
        out.printf ("Actual cost: %.2f%n", computePayment ()); 
        out.printf ("Discount (%d%): %.2f%n", getDiscount (), setDiscountPrice ()); 
        out.printf ("Amount to pay: %.2f%n", getPayment ());
    }

}

class LAB_3 
{
    public static void main (String [] args)
    {
        // reader
        Scanner input = new Scanner (in);

        // Declarations
        String station, type;
        double quantity, price, payment, discountPrice, netPayment;
        int discount;

        // constructor
        PetrolPurchase purchaseData = new PetrolPurchase (station, type, quantity, 
                                                          price, discount, payment,
                                                          discountPrice, netPayment);
        purchaseData.setInfo ();
        purchaseData.printInfo ();
    }       
}

共 (1) 个答案

  1. # 1 楼答案

    是的,您的变量没有初始化。 您声明了它们,但没有赋值

    根据您的代码:

        String station; // declaration
        station = "New York Central"; // assigment
    
        String type = "Main Station"; // declaration & assigment