计算一年内最低月度信用卡还款的代码

时间:2016-01-28 16:03:10

标签: python

请我试图找出我的推理有什么问题,以及我的结果。 我正在研究一个在线课程,我应该计算消除12个月内信用卡债务所需的最低数字。我获得了年利率,债务金额(余额)的价值,以及每月付款应该增加的值(10的倍数)。 根据我的推理,我生成的代码应该在几个月内迭代,但如果余额值不为零,则应增加每月付款并重新计算。 我的价值观(我认为)略微偏离了预期的结果。我的代码是这样的:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Gui extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel basic_panel, card_Layout_panel,
            banner_panel, welcome_authenticaion_panel_card1;
    private CardLayout basic2;
    private JLabel logo_label, name_label;

    public Gui() {
        server_login_gui();
        add(basic_panel);
        standard_gui();
    }

    public void server_login_gui() {

        basic_panel = new JPanel();
        basic_panel.setLayout(new BorderLayout(10, 10));
        basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
        banner_panel = new JPanel();
        //banner_panel.setLayout(null);
        banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
        banner_panel.setSize(680, 200);//(400,100,400,100);
        //////Banner inner things//////////////////////////////////////////////////
        logo_label = new JLabel("Logo");
        //logo_label.setBounds(30, 40, 100, 100);
        logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
        banner_panel.add(logo_label);
        name_label = new JLabel("        Name.....   ");
        name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC, 25));
        //name_label.setBounds(200, 80, 400, 50);
        name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
        banner_panel.add(name_label);
        ////////////////////////////////////////////////////////////////////////      
        basic_panel.add(banner_panel, BorderLayout.NORTH);
        ///////// Card Layout//////////////
        basic2 = new CardLayout();
        card_Layout_panel = new JPanel(basic2);
        card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
        basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
        welcome_authenticaion_panel_card1 = new JPanel();
        welcome_authenticaion_panel_card1.setLayout(null);
        welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
        //welcome_authenticaion_panel_card1.setBounds(0, 200, 680, 460);
        card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
        basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
        /////////////////////////////////////////////////////////////////////////
    }

    public void standard_gui() {
        setSize(700, 700);
        setTitle("System");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Gui();
            }
        });
    }
}

余额= 3329,年利率0.2, 我的结果是:310(正确)

余额= 4773,年利率0.2, 我的结果是:380(不正确,应该是440)

余额= 3926,年利率0.2, 我的结果是:340(不正确,应该是360)。

请有人帮我启发,我错了吗?

谢谢!

4 个答案:

答案 0 :(得分:2)

你几乎就在那里。您的实施中存在一些问题。

首先,您需要在实现之前测试的每月付款之后重置余额,以及付款。

其次,检查平衡并增加平衡的标签是错误的。按照目前的情况,你每月每月支付10美元,如果我能正确理解你的问题并不是你想要的。你希望在之后增加每月付款看到10美元以下没有在12个月内还清。

正如另一点,你的else: break是不必要的,因为它会在进入下一次迭代时突破while循环。

startBalance = int(input("what's the stating balance? "))
balance = startBalance
numMonths = 12

annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12.0
monthlyPayment = 10

while (balance > 0):
    balance = startBalance # reset the balance each iteration

    print('checking monthly payment of',monthlyPayment)
    for each in range(0, numMonths):
        balance = balance - monthlyPayment
        balance = balance + (monthlyInterestRate * balance)
        # print('at month',each,'the balance is',balance)

    # changed the indentation below
    if (balance > 0):
        monthlyPayment += 10

print('you should pay',monthlyPayment,'per month')

答案 1 :(得分:1)

  1. 支付的余额应该是全年相同的,因此if内的for each没有意义。 if不需要ReadOnly=0

  2. 尝试每月新付款时,余额需要重设为起始值。

  3. 我尝试了这些更改并且它与您的测试用例匹配。

答案 2 :(得分:1)

这个怎么样:

annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12.0
monthlyPayment = 10
running = true;

while (running):

    currentBalance = balance

    for each in range(0, 12):
        currentBalance = currentBalance - monthlyPayment
        currentBalance = currentBalance + (monthlyInterestRate * currentBalance)
    if (currentBalance > 0):
        monthlyPayment += 10
    else:
        running = false

print monthlyPayment

我基本上做的是从for-each中获取if条件,并使用副本余额。 while(running)基本上迭代了monthlyPayment的可能值。

(如果之前设置了currentBalance,你可以使用while(currentBalance> 0),但我会使用我的while(running)方法,因此它被读取为do-until-loop)

答案 3 :(得分:0)

Outstanding = 59400 # Total Outstanding Amount
interestrate = 4.2  # Interest Rate per month+ GST

#print("Month", "\t", "Interest+GST", "\t\t", "MinAmtDue", "\t\t\t", "Balance")
#print("-----", "\t\t", "-------", "\t\t", "--------", "\t\t\t", "-------")

month = 0
totpmt = 0
interest_GST = 0
minamtdue = 0
outstandingamt1 = Outstanding
while (outstandingamt1 + interest_GST - minamtdue) > 0 :
    month += 1
    interest_GST = outstandingamt1*4.2/100
    minamtdue = outstandingamt1 * 5/100
    #minamtdue = 12000
    outstandingamt1 = outstandingamt1 + interest_GST - minamtdue
    #print(month, "\t\t\t", round(interest_GST,2), "\t\t", round(minamtdue,2), "\t\t\t", round(outstandingamt1,2))
    totpmt = totpmt + minamtdue
#print(month+1, "\t\t\t", 0, "\t\t", round(outstandingamt1,2), "\t\t\t", 0)
print("Total Amount to be paid in ", month+1, "months= ", round(totpmt+outstandingamt1 , 2))