這是一些在fields.getText()中聲明值之後的模式,它通過遞歸函數計算公式。但是,java並不喜歡在actionPerformed按鈕下有任何'public'。我從來沒有使用過其中之一......但是您認爲ActionListener可以完成這項工作嗎?有任何想法嗎?如何在actionPerformed按鈕中加入'public int'?
private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
public int Method() { //underlined red
int n = Integer.parseInt(objectsChooseField.getText());
int r = Integer.parseInt(chooseFromField.getText());
int result = C(n, r);
}
public int C(int n, int r) { //underlined
int res = faculty(n)/(faculty(r) * (n - r));
return res;
}
public int faculty(n) { //underlined
if (n > 1)
return n * faculty(n - 1);
return 1;
}
}
編輯:
我想我可能已經採取了喬的回答有點字面上...但你的意思是這樣的......?
public int Method() { //moved above actionPerformed
private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
int n = Integer.parseInt(objectsChooseField.getText()); //continued method?
int r = Integer.parseInt(chooseFromField.getText());
int result = C(n, r);
}
//rest of code below
您正試圖在方法中聲明方法 – Joan 2013-05-11 18:56:29
作爲便箋,我認爲您尋找的函數名是'factorial',而不是'faculty' ... – ValarDohaeris 2013-05-11 18:58:13
您是否擁有JavaScript背景? – skuntsel 2013-05-11 18:58:23