2017-10-17 66 views
-1

我正在學習一個Java類,並且我一直在研究這個程序,該程序基於通過GUI輸入的信息計算信用卡餘額。在我的輸出財務費用,新餘額和新付款到期日均爲0.我嘗試添加5以查明我在哪裏搞砸了,並且在計算newBalance時似乎沒有正確引用「餘額」。我認爲財務費用也有同樣的問題,但固定餘額也會幫助我解決這個問題。用一種方法計算的變量不會去另一個

//calculates balance 
     public float calculateBalance() 
     { 
      balance = previousBalance + currentPurchases - payments - creditsReturns + lateFees+ 5; 

      return balance; 
     } 

     //sets finance charge 
     public void setFinanceCharge(float financeCharge) 
     { 
      double periodicRate; 
      periodicRate = .12/12; 
      float d = (float)periodicRate; 
      financeCharge = balance * d; 
     } 

     //gets finance charge 
     public float getFinanceCharge() 
     { 
      return financeCharge; 
     } 

    //Method to calculate new balance 
    public float calculateNewBalance() 
    { 
     //calculate the new balance 
     newBalance = balance+financeCharge+5; 

     return newBalance; 
    } 

    //setes new payment due 
    public void setpaymentDue(double newPayment) 
    { 
     newPayment = newBalance * .10; 
     this.paymentDue = (float)newPayment;    
    } 

    //gets new payment due 
    public float getpaymentDue() 
    { 
     return paymentDue; 
    } 

    //method to display results 
    public void displayOutput() 
    { 
     if (overCreditLimit == 0) 
     { 
     JOptionPane.showMessageDialog(null, 
       "The Customer number is: " + customerNumber + "\n" + 
       "The Customer name is: " + customerName + "\n" + 
       "The Credit Limit is: " + creditLimit + "\n" + 
       "The Previous Balance is: " + previousBalance + "\n" + 
       "The Current Purchases is: " + currentPurchases + "\n" + 
       "The Payments is: " + payments + "\n" + 
       "The Credits/Returns is: " + creditsReturns + "\n" + 
       "The Late Fees is: " + lateFees + "\n" + 
       "The Finance Charge is: " + financeCharge + "\n" + 
       "The New Balance is: " + newBalance + "\n" + 
       "The New Payment Due is: " + paymentDue + "\n"); 
     } 
     else 
     { 
      overCreditAmount = newBalance - creditLimit - 25; 
      JOptionPane.showMessageDialog(null, "You are " + overCreditAmount + " dollars over your credit limit," 
        + " a $25 fee has been charged to your new balance"); 
      JOptionPane.showMessageDialog(null, 
        "The Customer number is: " + customerNumber + "\n" + 
        "The Customer name is: " + customerName + "\n" + 
        "The Credit Limit is: " + creditLimit + "\n" + 
        "The Previous Balance is: " + previousBalance + "\n" + 
        "The Current Purchases is: " + currentPurchases + "\n" + 
        "The Payments is: " + payments + "\n" + 
        "The Credits/Returns is: " + creditsReturns + "\n" + 
        "The Late Fees is: " + lateFees + "\n" + 
        "The Finance Charge is: " + financeCharge + "\n" + 
        "The Amount over Credit Limit is: " + overCreditAmount + "\n" + 
        "The New Balance is: " + newBalance + "\n" + 
        "The New Payment Due is: " + paymentDue + "\n"); 
     } 
    } 
+0

爲了解決這個問題,我建議運行calculateNewBalance中的方法而不是調用變量。例如,爲了達到平衡,請調用calculateBalance(),看看會發生什麼。 – OneSurvivor

+0

如果它不起作用,我會建議在方法中創建一個本地變量以取代平衡,並查看它是否有效。如果沒有,那麼您沒有顯示的代碼可能存在問題。 – OneSurvivor

回答

0

我看到解決此問題的一種方法是在calculateNewMethod方法內運行方法。例如。

public float calculateNewBalance() { 
    newBalance = calculateBalance() + getFinanceCharge(); 
    return newBalance; 
} 

我也建議避免在您修改私有類變量下和同樣的方法內返回他們的代碼,它更有意義,有一個方法,修改它,以及一個返回varible。

相關問題