我已經創建了一個業務程序,它帶有一個循環的double值並計算淨利潤。我需要將主類中的輸入值添加到名爲Business的自定義類中。然後,我應該計算Business類中的淨利潤並將最終值輸出到主類。當我運行我的當前程序時,結果是「0.0」。 Business類沒有從我的主類獲取我的輸入值,但我找不出原因。下面主要類:爲什麼我的程序返回「0.0」?
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;
}
}
第二個自定義類別:
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)));
}
}
}
'商務類不是從我的主類讓我的輸入值,但我可以」不知道爲什麼 - 你沒有做任何事情來傳遞值,那麼你爲什麼期望通過輸入? – Eran