我在这个练习中遇到了一点问题,我想知道是否有人可以提供帮助。这是问题所在:
创建一个名为 Purchase 的类。每个采购都包含一个发票编号、销售金额和销售税金额。包括发票编号和销售额的设置方法。在销售金额的 set() 方法中,将销售税计算为销售金额的 5%。还包括显示购买详细信息的显示方法。另存为 Purchase.class b。创建一个声明购买对象并提示用户提供购买详细信息的应用程序。当您提示输入发票编号时,在输入 1,000 到 8,000 之间的数字之前不要让用户继续操作。当您提示输入销售额时,在用户输入非负值之前不要继续。创建有效的采购对象后,显示该对象的发票编号、销售额和销售税。
这是我的购买课程的代码
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Purchase
{
//variables
public static int invoice;
public static double saleAmount;
public static double saleTax;
//get&set for Invoice
public void setInvoice(int x)
{
invoice = x;
}
public int getInvoice( )
{
return invoice;
}
//get&set for saleAmount
public void setSaleAmount(double y)
{
saleTax = y * 0.05;
saleAmount = y;
}
public double getSaleAmount( )
{
return saleAmount;
}
//get for saleTax
public double getSaleTax( )
{
return saleTax;
}
//display method
public void display(int invoice, double saleAmount, double saleTax)
{
System.out.println("Invoice number: " + invoice + '\n' + "Sale's Amount: " + saleAmount + '\n' + "Sale's Tax: " + saleTax);
}
}
以及 CreatePurchase 类的代码
import javax.swing.JOptionPane;
import java.util.Scanner;
public class CreatePurchase
{
public static void main(String[] args)
{
Purchase purchase1 = new Purchase ();
//scanner for sales amount
Scanner inputDevice = new Scanner(System.in);
System.out.println("Please enter the sale amount: ");
Purchase.saleAmount = inputDevice.nextDouble();
//loop for saleAmount
while (Purchase.saleAmount < 1)
{
System.out.print('\n'+ "Error, your sale amount needs to be more than 0. Please enter a valid sale amount: >> ");
Purchase.saleAmount = inputDevice.nextDouble();
}
//scanner for invoice
System.out.println("Please enter an invoice number between 1000 and 8000: ");
Purchase.invoice = inputDevice.nextInt();
//loop for invoice
while (Purchase.invoice < 999 || Purchase.invoice > 8000)
{
System.out.print('\n'+ "Error, please enter a valid invoice number between 1000 and 8000: >> ");
Purchase.invoice = inputDevice.nextInt();
}
//display result
JOptionPane.showMessageDialog(null, "Your invoice number is " + Purchase.invoice + '\n'
+ "Your sale tax is: " + Purchase.saleTax + '\n'
+ "Your grand total is: " + Purchase.saleAmount);
}
}
如您所见,当您运行第二类时,saleAmount 不包括额外的 5% 的销售税,销售税仍然为 0。可能真的很愚蠢,但我不知道从哪里开始。