0

我目前正在研究 Java 复利计算器。我对 Java 非常陌生,这个项目非常具有挑战性,现在我发现自己陷入了困境。

我担心我在做一些我应该知道的根本错误的事情(我把东西放在错误的地方了吗?)

我的行旁边出现“非法类型开始”错误:

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

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

帐户.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;
}
}

银行家.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);
}


}
4

1 回答 1

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;
    }

}
于 2012-11-08T22:41:40.027 回答