Java复合利息计算器

时间:2012-11-08 22:39:52

标签: java

我目前正在研究Java复利计算器。我对Java非常陌生,这个项目非常具有挑战性,现在我发现自己处于一堵砖墙。

我担心我做的事情根本就是我应该知道的错误(我把错误的东西放了吗?)

我的行旁边有一个'非法的类型开头'错误:

for ( int i = 0; i<period; i++)

我想我可能需要将它放在方法中?这是我的完整代码。

Account.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Account extends JFrame {

private int period;
private int balance;
private int rate;
private String printstring;

@Override
public String toString() {
    return String.format("Period: " + period + ", Balance: " + balance);
}

public int getPeriod() {
    return period;
}

public void setPeriod(int period) {
    this.period = period;
}

public int getBalance() {
    return balance;
}

public void setBalance(int balance) {
    this.balance = balance;
}

public int getRate() {
    return rate;
}

public void setRate(int rate) {
    this.rate = rate;
}

public String getPrintstring() {
    return printstring;
}

public void setPrintString(String printstring) {
    this.printstring = printstring;
}


for ( int i = 0; i<period; i++)
{
    balance = (balance * rate) + balance;
}
}

Banker.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Banker {

private static final int H = 400;       // Height of window pixels
private static final int W = 500;       // Width  of window pixels

public static void main(String[] args) {

    String period;
    String balance;
    String rate;

    period = JOptionPane.showInputDialog(null, "Interest period:");
    balance = JOptionPane.showInputDialog(null, "Current balance:");
    rate = JOptionPane.showInputDialog(null, "Interest rate:");
    JFrame frame = new JFrame("Savings Interest Calculator");
    JButton calculate = new JButton("Calculate");
    JLabel blabel = new JLabel("Period: " + period);
    JLabel plabel = new JLabel("Balance: " + balance);
    JLabel flabel = new JLabel("Balance: " + balance);
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(calculate);
    frame.add(plabel);
    frame.add(blabel);
    frame.pack();
    frame.setVisible(true);
}


}

1 个答案:

答案 0 :(得分:0)

你必须写

 for ( int i = 0; i<period; i++)
{
    balance = (balance * rate) + balance;
}

在方法体内。你不能在你的方法之外写出这样的陈述。把它写在像下面这样的方法体中。

public void myMethod() {
     for ( int i = 0; i<period; i++)
    {
        balance = (balance * rate) + balance;
    }

}