简体   繁体   English

为什么我的程序返回“0.0”?

[英]Why is my program returning "0.0"?

I have created a business program that takes in double values with a loop and calculates net profit.我创建了一个业务程序,该程序通过循环接收双倍值并计算净利润。 I am required to add the input values from the main class to a custom class called Business.我需要将来自主类的输入值添加到名为 Business 的自定义类中。 Then I am supposed to calculate the net Profit in the Business class and print the final value to the main class.然后我应该计算 Business 类中的净利润并将最终值打印到主类。 When I run my current program, the result is "0.0".当我运行我当前的程序时,结果是“0.0”。 The Business class is not getting my input values from my main class, but I can't figure out why. Business 类没有从我的主类中获取我的输入值,但我不知道为什么。 Main class below:下面的主要类:

public class BusinessProject {

public static double revenue;
public static double expenses;
public static double TotalRevenue;
public static double TotalExpenses;

public static void main(String[] args) {

    Business calc = new Business();

    getTotalRevenue();
    getExpense();
    calc.Profit();

}   
public static double getTotalRevenue() {    
    Scanner scan = new Scanner(System.in);

    while (true) {
        System.out.println("Enter your revenue: \nJust type 0 when you've finished inputting all values");
        revenue = scan.nextDouble();
        TotalRevenue += revenue;


    if(revenue==0) {

        break;
    }
}
    return TotalRevenue;
}
public static double getExpense() { 

    Scanner scan = new Scanner(System.in);

    while (true) {
        System.out.println("Enter your expenses: \nJust type 0 when you've finished inputting all values");
        expenses = scan.nextDouble();
        TotalExpenses += expenses;

    if(expenses==0) {

        break;

        }
    }
    return TotalExpenses;
}
}

Second Custom Class:第二个自定义类:

public class Business {

public static double ExpenseInput;
public static double RevenueInput;

public void REVENUE() {

    BusinessProject TOTAL = new BusinessProject();

    double RevenueInput = BusinessProject.TotalRevenue;

}

public static void EXPENSE() {

    BusinessProject TOTAL2 = new BusinessProject();

    double ExpenseInput = BusinessProject.TotalExpenses;
}

public void Profit() {

    double difference = (RevenueInput - ExpenseInput);

    if (difference <=1000) {

        System.out.println("Net Profit: " + (difference - (difference * 0.00175)));
    }

}

}

You get 0.0 because you haven't called your methods to set RevenueInput and ExpenseInput.你得到 0.0,因为你没有调用你的方法来设置 RevenueInput 和 ExpenseInput。

So in your case calling EXPENSE() and REVENUE() before profit() would solve it.所以在你的情况下,在利润()之前调用 EXPENSE() 和 REVENUE() 可以解决它。

However, I'd advice you to look over your program structure and naming convention.但是,我建议您查看您的程序结构和命名约定。 You could either pass the variables as arguments for your function such as: Profit(double expense, double revenue) or you could have it in the constructor of Business like so: public Business(double expense, double revenue)您可以将变量作为函数的参数传递,例如: Profit(double expense, double revenue)或者您可以在 Business 的构造函数中使用它,如下所示: public Business(double expense, double revenue)

What you have right now is a circular dependency where you are relying on static variables in the class(BusinessProject) that your object(Business) then uses.您现在拥有的是一个循环依赖项,您依赖于您的对象(Business)然后使用的类(BusinessProject)中的静态变量。

I would personally refactor it like this:我个人会像这样重构它:

public class Business {
    public static void profit(final double revenue, final double expense) {
        double difference = (revenue - expense);

        if (difference <=1000) {

            System.out.println("Net Profit: " + (difference - (difference * 0.00175)));
        }

Then from your main project you simply call Business.profit(TotalRevenue, TotalExpense);然后从您的主要项目中,您只需调用Business.profit(TotalRevenue, TotalExpense);

Instead of passing Business object you are creating new Business object in main class.您不是传递Business对象,而是在主类中创建新的Business对象。 Hence properties will be initialized with default values因此属性将使用默认值初始化

You have to call EXPENSE() and Profit() from main class and you have to pass Business class as parameter to those methods您必须从主类调用EXPENSE()Profit() ,并且必须将Business类作为参数传递给这些方法

public void Profit(BusinessProject bp) {

    double difference = (bp.TotalRevenue - bp.TotalExpenses);

    if (difference <=1000) {

        System.out.println("Net Profit: " + (difference - (difference * 0.00175)));
    }

}

and call it as below并将其称为如下

calc.Profit(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM