我遇到的問題是從Java中的「2d數組」中檢索值。我知道在Java中並不存在像2d數組那樣的東西,但是和我一樣。我對Java非常陌生,正在爲我的課程開發一個項目。我應該將jTextField中的信息提交給一個數組,然後將存儲在數組中的信息備份到一個彙總報告中。這是我到目前爲止有:從Java中的「2d數組」中檢索值的問題
public class KETTask1UI extends javax.swing.JFrame {
//Creates the "2d array" to store minutes worked and payment values
int[][] paymentArray = new int [20][2];
/**
* Creates new form KETTask1UI
*/
public KETTask1UI() {
initComponents();
}
private void runreportActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
for (int i=0; i<paymentArray.length; i++) {
for (int j=0; j<paymentArray[i].length; j++) {
//Blanks out the displayTextArea
displayTextArea.setText(" ");
//Displays the contents of the array in the displayTextArea
displayTextArea.append("History of minutes and payment entered:\n");
displayTextArea.append(String.valueOf(paymentArray[i][j]));
}
}
}
private void minutesActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void quitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int min = Integer.parseInt(minutes.getText());
int pay = Integer.parseInt(payment.getText());
for (int i=0; i<paymentArray.length; i++) {
for (int j=0; j<paymentArray[i].length; j++) {
//Sets the amount of minutes worked and payment to the array
paymentArray[i][0] = min;
paymentArray[i][1] = pay;
//display a message to the user to let them know the values that they've entered.
displayTextArea.append("******************\nRaw Tutoring Earnings Data\n\nMinutes Earnings\n");
displayTextArea.append(String.valueOf(paymentArray[i][j]));
displayTextArea.append("\n\n******************");
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new KETTask1UI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextArea displayTextArea;
private javax.swing.JButton enterButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField minutes;
private javax.swing.JTextField payment;
private javax.swing.JLabel paymentLabel;
private javax.swing.JButton quit;
private javax.swing.JButton runreport;
private javax.swing.JLabel timeLabel;
// End of variables declaration
}
現在,現在,當我點擊進入按鈕,一切都是爲了給我的是下面的(30是分鐘的最後一個數字20是用於支付的最後一個數字):
******************
Raw Tutoring Earnings Data
Minutes Earnings
30
************************************
Raw Tutoring Earnings Data
Minutes Earnings
20
此消息在textArea中重複20次。如果我輸入更多的數據,它只是用新數字做同樣的事情。
我需要它看起來像這樣:
******************
Raw Tutoring Earnings Data
Minutes Earnings
20 30
******************
當我輸入新的價值觀和再按下回車鍵,我需要它來顯示一個更新列表如下:
******************
Raw Tutoring Earnings Data
Minutes Earnings
20 30
30 15
******************
任何非常感謝幫助。謝謝。
什麼是輸出與預期輸出? –
對於什麼是值得的,這是一個「好」的方式來做到這一點的情況是有一個類型的對象數組。這裏的「行」並不是真正的同一事物的不同事件列表。 [i] [0]是分鐘,[i] [1]是付款,[i] [2]如果它存在可能是別的。 –
這個錯誤看起來像是2D陣列問題的一個不同的問題,你可以發佈在KETTask1UI(第47行)發生了什麼?你在actionPerformed方法中出錯。它也可能是NetBeans,檢查http://stackoverflow.com/questions/4386076/uncompilable-source-code-runtimeexception-in-netbeans – hotforfeature