Java 你能解释一下为什么我的班不会回到主程序吗?

Java 你能解释一下为什么我的班不会回到主程序吗?,java,Java,我的作业如下 挑战。。。 该程序应该询问售出的单位数量,并计算购买的总成本。 我还应该包括一个输入验证,以便单元数大于0。 套餐价格为99美元,数量折扣为: 10-19 20% 20-49 30% 50-99 40% 100%或以上50% 我有我的类演示,我必须用它来测试我的代码 package Chapter4; import java.text.DecimalFormat; import java.util.Scanner; public class Soft

我的作业如下

挑战。。。 该程序应该询问售出的单位数量,并计算购买的总成本。 我还应该包括一个输入验证,以便单元数大于0。 套餐价格为99美元,数量折扣为: 10-19 20% 20-49 30% 50-99 40% 100%或以上50%

我有我的类演示,我必须用它来测试我的代码

    package Chapter4;
    import java.text.DecimalFormat;
    import java.util.Scanner;
    public class SoftwareSalesDemo {

public static void main(String[] args) {
    int units; // To hold units sold

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Get the units sold.
    System.out.print("Enter the units sold: ");
    units = keyboard.nextInt();

    // Create a SoftwareSales object.
    SoftwareSales sales = new SoftwareSales(units);

    // Display purchase info.
    DecimalFormat dollar = new DecimalFormat("#,##0.00");
    System.out.println("Units sold: " + sales.getUnitsSold());
    System.out.println("Discount: $" + dollar.format(sales.getDiscount()));
    System.out.println("Cost: $" + dollar.format(sales.getCost()));

}

}
下面是我写的课程,这是我需要帮助的

package Chapter4;

/*
 * Software sales class
 * 
 * Eric Goldberg
 * 
 * Calculate the total cost of units sold */

public class SoftwareSales {

//fields 
private int UnitsSold;
private double Discount;
private double Cost;
private double UnitCost=99.00;


public SoftwareSales (int uni) 
{
    UnitsSold = uni;

}



public Discount(double dis)
{
    Discount = dis;

    if(UnitsSold >=10 && UnitsSold<=19) {
        Discount = .20;
    }

    if(UnitsSold >= 20 && UnitsSold<=49) {
        Discount = .30;
    }

    if (UnitsSold >= 50 && UnitsSold <= 99) {
        Discount = .40;
    }

    if (UnitsSold >= 100) {
        Discount = .50;
    }

} //end of discount

public double UnitCost() {
    return UnitCost=99.00;
}

public  void setCost(double cost) {
    Cost = Discount*UnitsSold;
}

//return the discount amount. 
public double getDiscount() 
{
    return Discount;
}

//return unitssold. 
public int getUnitsSold() {
    return UnitsSold;
}

//return cost
public double getCost() {
    return Cost;
}

}

您需要更正成本和折扣计算。我想你们需要总成本和总折扣。您可以根据自己的需要对输入的单位进行验证。比如读字符串,然后解析int。现在,我假设输入是一个整数,直到用户输入integer>0,我才继续询问

SoftwareSalesDemo.java

package Chapter4;

import java.text.DecimalFormat;
import java.util.Scanner;

public class SoftwareSalesDemo {

    public static void main(String[] args) {
        // To hold units sold
        int units;

        // Display purchase info.
        DecimalFormat dollar = new DecimalFormat("#,##0.00");

        // Get the units sold.
        System.out.print("Enter the units sold: ");

        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);
        units = keyboard.nextInt();

        while (units <= 0) {
            System.out.println("Please enter valid no. of units (>0)");
            units = keyboard.nextInt();
        }
        // Create a SoftwareSales object.
        SoftwareSales sales = new SoftwareSales(units);
        System.out.println("Units sold     : " + sales.getUnitsSold());
        System.out.println("Total Discount : $" + dollar.format(sales.getDiscount()));
        System.out.println("Total Cost     : $" + dollar.format(sales.getCost()));

    }
}

确切的问题是什么?您的代码不应该编译,因为public Discountdouble dis是一个构造函数,不在折扣类中。您的许多方法从未被调用。此外,这个Cost=Discount*UnitsSold看起来不正确。您可以调用getCost,但从不调用setCost。请阅读Java命名约定。变量名就这样。你真的认为,给出一个完整的答案是帮助某人学习编程的最好方法吗?我的坏意图是给出解决方案并显示缺失的部分。也许下一次我只是提供一些提示。
package Chapter4;

/*
 * Software sales class
 * 
 * Eric Goldberg
 * 
 * Calculate the total cost of units sold */

public class SoftwareSales {

    // fields
    private int unitsSold;
    private double discount;
    private double cost;
    private double unitCost = 99.00;

    public SoftwareSales(int units) {
        this.unitsSold = units;
        setDiscount();
        setCost();
    }

    // set discount based on no. of units sold
    private void setDiscount() {
        if (unitsSold >= 10 && unitsSold <= 19) {
            discount = .20;
        }

        if (unitsSold >= 20 && unitsSold <= 49) {
            discount = .30;
        }

        if (unitsSold >= 50 && unitsSold <= 99) {
            discount = .40;
        }

        if (unitsSold >= 100) {
            discount = .50;
        }
    } // end of discount

    // return per unit cost
    private double getUnitCost() {
        return unitCost;
    }

    // calculate total cost
    private void setCost() {
        cost = unitsSold * (getUnitCost() * (1 - discount));
    }

    // return the discount amount.
    public double getDiscount() {
        return ((discount * getUnitCost()) * unitsSold);
    }

    // return unitsSold.
    public int getUnitsSold() {
        return unitsSold;
    }

    // return total cost
    public double getCost() {
        return cost;
    }
}
Enter the units sold: 10
Units sold     : 10
Total Discount : $198.00
Total Cost     : $792.00

Enter the units sold: 50
Units sold     : 50
Total Discount : $1,980.00
Total Cost     : $2,970.00

Enter the units sold: 100
Units sold     : 100
Total Discount : $4,950.00
Total Cost     : $4,950.00