2012-11-08 108 views
0

我目前正在研究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); 
} 


} 
+2

for循環需要在方法內。 – assylias

+0

您可以計算複利,不需要循環。 http://qrc.depaul.edu/StudyGuide2009/Notes/Savings%20Accounts/Compound%20Interest.htm –

回答

0

你必須寫

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

的方法體內。你不能在你的methods.try中寫出這樣的語句,把它寫在下面的方法體內。

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

}