刚刚赢得的变量初始化';t工作(Java类)
需要java作业方面的帮助,我无法编译代码。 我的家庭作业基本上是想让我创建一个名为PetrolPurchase的类来表示您在另一个类中购买的汽油的相关信息。 给出的错误是我的变量可能尚未初始化。 很抱歉粘贴了我的整个文件,但感觉如果我不这样做,上下文将很难理解刚刚赢得的变量初始化';t工作(Java类),java,class,Java,Class,需要java作业方面的帮助,我无法编译代码。 我的家庭作业基本上是想让我创建一个名为PetrolPurchase的类来表示您在另一个类中购买的汽油的相关信息。 给出的错误是我的变量可能尚未初始化。 很抱歉粘贴了我的整个文件,但感觉如果我不这样做,上下文将很难理解 import static java.lang.System.*; import java.util.Scanner; class PetrolPurchase { // reader Scanner input =
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 ();
}
}
是的,您的变量未初始化。 您声明了它们,但没有赋值 根据您的代码:
String station; // declaration
station = "New York Central"; // assigment
String type = "Main Station"; // declaration & assigment
对于这个赋值,我不能简单地通过赋值来初始化它们,我必须输入用户输入数据,但我甚至不能进入那个阶段,因为我的代码无法编译,并且显示了PetrolBurchase purchaseData中的参数可能不正确的错误initialized@Mandroid我可以知道你加了什么套餐单吗?我只是不能把它编译成你需要使用你代码中的Scanner对象。字符串input1=input.nextLine();。。。。您可以在这里看到它的用法:忽略我的第一条注释。请参见上面的一条。PetrolPurchase类中不需要Scanner对象。您的代码没有对
输入执行任何操作。在将这些变量传递给PetrolPurchase构造器之前,您可能应该读取一些值并将其存储在站
、类型
、数量
等中。