我的 display() 不是 运行 或显示我的 println

my display() is not running or showing my println

我正在处理的程序目前运行成功,但它没有执行我程序的一部分,也没有显示任何错误。

提示是: "Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1000 and 8000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a non-negative number, sale amount, and sales tax. Save the file as CreatePurchase.java."

import java.util.Scanner;

public final class CreatePurchase {
    static int invoiceNum; // I made these variables static because
    static double amount; // it fixed an error I was having with using
                            // non-static variables in a static field?

    double tax = .05;
    double totalamount;

    public static void main(String[] args) {
        Purchase completedPurchase = new Purchase();
        Scanner input = new Scanner(System.in);
        System.out
                .println("Please enter the invoice number! (Must be between 1000 and 8000!)");
        invoiceNum = input.nextInt();
        System.out
                .println("Please enter the amount of the sale! (Must be greater than 0!)");
        amount = input.nextDouble();

        while (invoiceNum < 1000 || invoiceNum > 8000) {
            System.out
                    .println("The invoice number you entered is invalid, try again!");
        }
        while (amount < 0) {
            System.out.println("The sale amount is invalid, try again!");
        }
    }

    public int getInvoiceNum(int invoiceNum) {
        return invoiceNum;
    }

    public double getAmount(double amount) {
        return amount;
    }

    public double getTotalAmount(double totalAmount) {
        return totalAmount;
    }

    public void setTotalAmount(double tax, double amount) {
        double totalAmount = (amount * tax);
    }

    public void display(int invoiceNum, double amount, double totalAmount) {
        System.out.println("Your invoice number is:" + invoiceNum + ".");
        System.out.println("Your sale amount is: " + amount + ".");
        System.out.println("Your sale amount after tax is: " + totalAmount
                + ".");
    }
}

我花了几个小时试图弄清楚我需要修复什么才能使我的显示方法正常工作。程序成功运行,没有错误,所以我什至不确定要尝试修复什么。 (抱歉,如果我的代码或问题没有意义。)

如果有机会,我会大量重构您的代码。首先,CreatePurchase class 应该是一个简单的 POJO(普通旧 Java 对象),包含发票编号、购买金额和销售税的状态,以及 getter和 setter 访问和更改该状态的方法。接下来,此 class 中的 main() 方法将实例化一个 CreatePurchase 对象来存储用户输入。我所做的一个重大改变是代码处理用户输入的方式。我下面的代码使用两个 while 循环来轮询用户以获得正确的发票编号(在 10008000 之间)和金额(非负数),然后才继续执行其余部分方法。最后使用创建的CreatePurchase对象将结果输出到控制台

public final class CreatePurchase {
    private int invoiceNum;
    private double amount;
    private double totalamount;

    // I am hard-coding the sales tax as 5%, as you did in your question,
    // though this code can easily be modified if you also want to input the tax
    private final double tax = .05;

    public int getInvoiceNum() {
        return invoiceNum;
    }

    public void setInvoiceNum(int invoiceNum) {
        this.invoiceNum = invoiceNum;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public static void main(String[] args) {
        CreatePurchase cp = new CreatePurchase();
        Scanner input = new Scanner(System.in);

        // these next two do-while loops will continue polling the user
        // until he enters a valid input
        do {
            System.out.println("Please enter the invoice number! (Must be between 1000 and 8000!)");
            invoiceNum = input.nextInt();
        } while (invoiceNum < 1000 || invoiceNum > 8000);

        cp.setInvoiceNum(invoiceNum);

        do {
            System.out.println("Please enter the amount of the sale! (Must be greater than 0!)");
            amount = input.nextDouble();
        } while (amount < 0);

        totalamount = amount*(1 + tax);
        cp.setAmount(amount);
        cp.setTotalAmount(totalAmount);

        // now use the CreatePurchase object to print out
        // details of the transaction
        System.out.println("Your invoice number is:" + cp.getInvoiceNum() + ".");
        System.out.println("Your sale amount is: " + cp.getAmount() + ".");
        System.out.println("Your sale amount after tax is: " + cp.getTotalAmount() + ".");
    }
}

您声明了方法 public void display(int invoiceNum, double amount, double totalAmount) 但您从未真正使用过它。

试试下面的代码,应该会有帮助!

import java.util.Scanner;

public final class CreatePurchase {
    static int invoiceNum; // I made these variables static because
    static double amount; // it fixed an error I was having with using
                            // non-static variables in a static field?

    static double tax = .05;
    static double totalAmount;

    public void updatePurchase(final Purchase purchase) {
        Scanner input = new Scanner(System.in);
        System.out
                .println("Please enter the invoice number! (Must be between 1000 and 8000!)");
        invoiceNum = input.nextInt();

        if (invoiceNum < 1000 || invoiceNum > 8000) {
            System.out
                    .println("The invoice number you entered is invalid, try again!");
            updatePurchase(purchase);

        }
        System.out
                .println("Please enter the amount of the sale! (Must be greater than 0!)");
        amount = input.nextDouble();

        if (amount < 0) {
            System.out.println("The sale amount is invalid, try again!");
            updatePurchase(purchase);
        }

        setTotalAmount(tax, amount);

        display(invoiceNum, amount, getTotalAmount());

    }

    public static void main(String[] args) {
        Purchase completedPurchase = new Purchase();
        new CreatePurchase().updatePurchase(completedPurchase);
    }

    public int getInvoiceNum(int invoiceNum) {
        return invoiceNum;
    }

    public double getAmount(double amount) {
        return amount;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double tax, double amount) {
        totalAmount = (amount * tax);
    }

    public void display(int invoiceNum, double amount, double totalAmount) {
        System.out.println("Your invoice number is:" + invoiceNum + ".");
        System.out.println("Your sale amount is: " + amount + ".");
        System.out.println("Your sale amount after tax is: " + totalAmount
                + ".");
    }
}