因此,我收到了很多提示,並獲得了一些加入此社區後學習Java的資源。我現在已經到了班級的第6周,並正在完成第三個項目。我覺得我學到了很多,但如果我想掌握Java,我還有很長的路要走。將多個輸出保存到csv文件
這次我的問題是如何讓我的代碼保存多個輸出到文件?我的當前項目的
部分是做到以下幾點:
「當窗口被關閉,效率值應爲n>值來計算從0到10,並寫入到一個文件中的每個文件行>應該包含n的值,迭代方法的效率> n的值和遞歸方法的效率,值>應該用逗號分隔,以便可以用Excel打開文件。 「
我已經設法讓程序寫入輸出文件的單個條目。不過,我要麼在代碼中出錯,要麼缺少重要的東西。有人能指出我正確的解決方案嗎?我想我可能不得不創建一個數組,將輸出存儲在那裏,然後將數組輸出到csv。我看過roseindia和viralpatel,但那些沒有透露我希望的。
序列(部分我搞砸了)
package cmisproject3;
public class Sequence {
private static int efficiency = 0;
// method to compute iterative
public static int computeIterative(int n) {
int result = 0;
efficiency = 0;
if (n == 0) {
result = 0;
} else if (n == 1) {
result = 1;
} else {
int secondPrevious = 0;
int previous = 1;
for (int i = 2; i <= n; i++) {
efficiency++;
result = 2 * previous + secondPrevious;
secondPrevious = previous;
previous = result;
}
}
return result;
}
// method to comopute recursive
public static int computeRecursive(int n) {
efficiency = 0;
return computeRecursiveHelper(n);
}
private static int computeRecursiveHelper(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
efficiency++;
return 1;
} else {
efficiency++;
return 2 * computeIterative(n - 1) + computeIterative(n - 2);
}
}
public static int getEfficiency() {
return efficiency;
}
}
GUI(釘呢?)
package cmisproject3;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class CMISProject3 extends JFrame implements ActionListener {
private final int TWICE = 2;
private JLabel jLabel1 = new JLabel(), jLabel2 = new JLabel(), jLabel3 = new JLabel(), jLabel4 = new JLabel(), jLabel5 = new JLabel(), jLabel6 = new JLabel();
private ButtonGroup radioButtons = new ButtonGroup();
private JRadioButton iterativeBtn = new JRadioButton(), recursiveBtn = new JRadioButton();
private JTextField enterN = new JTextField(16), textResult = new JTextField(16), textEfficiency = new JTextField(16);
private JButton computeBtn = new JButton();
private int efficiency;
private Sequence sequence;
private static FileWriter fileWriter;
private File file = new File("output.txt");
// Beginning of the constructor for the GUI
public CMISProject3() throws IOException {
sequence = new Sequence();
setSize(300, 200); // define size of GUI
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(6, 2));
getContentPane().add(jLabel4);
radioButtons.add(iterativeBtn);
iterativeBtn.setSelected(true); // sets Iterative as default GUI selection
iterativeBtn.setText("Iterative");
getContentPane().add(iterativeBtn);
getContentPane().add(jLabel5);
radioButtons.add(recursiveBtn);
recursiveBtn.setText("Recursive");
getContentPane().add(recursiveBtn);
jLabel1.setText("Enter n: ");
getContentPane().add(jLabel1);
getContentPane().add(enterN);
getContentPane().add(jLabel6);
computeBtn.setText("Compute");
computeBtn.addActionListener(this);
getContentPane().add(computeBtn);
jLabel2.setText("Result: ");
getContentPane().add(jLabel2);
getContentPane().add(textResult);
textResult.setEditable(false);
jLabel3.setText("Efficiency: ");
getContentPane().add(jLabel3);
getContentPane().add(textEfficiency);
textEfficiency.setEditable(false);
pack();
}
public void actionPerformed(ActionEvent event) {
int result;
efficiency = 0;
try {
fileWriter = new FileWriter(file);
} catch (IOException e1) {
e1.printStackTrace();
}
if (iterativeBtn.isSelected()) {
result = sequence.computeIterative(Integer.parseInt(enterN.getText()));
} else {
result = sequence.computeRecursive(Integer.parseInt(enterN.getText()));
}
try {
System.out.println(result);
fileWriter.write(result + ", " + sequence.getEfficiency());
} catch (IOException e) {
e.printStackTrace();
}
textResult.setText(Integer.toString(result));
textEfficiency.setText(Integer.toString(sequence.getEfficiency()));
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
CMISProject3 CMISProject3 = new CMISProject3();
CMISProject3.setVisible(true);
}
}
對於那些有興趣,這裏是我工作範圍內的參數。 Instructions
所有左對齊的代碼==很難讀取代碼。你確定你想讓你的問題難以回答嗎? –