我想写入一个文件,但它似乎没有写入它,文件名是Grade output.txt...
例如,当我运行程序时,测验 1 的输入:90 .. 这是它应该在停止运行后添加到文件中的 Q1。
'try {
BufferedWriter out = new BufferedWriter(new FileWriter("Grade output.txt"));
//for (int i = 0; i < 11; i++) {
out.write(Q1 + " ");
System.out.println(Q1);
out.close();
} catch (IOException e) {}
'
'
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
公共类 AICS_grade_applet 扩展 Applet 实现 ActionListener {
// 标签 titleLabel = new Label("Grading System", Label.CENTER);
Label prelimsQ1Label = new Label("Quiz 1", Label.LEFT);
TextField prelimsQ1Field = new TextField(10);
Label prelimsQ2Label = new Label("Quiz 2", Label.LEFT);
TextField prelimsQ2Field = new TextField(10);
Label prelimsCSLabel = new Label("Quiz 3", Label.LEFT);
TextField prelimsCSField = new TextField(10);
Label prelimsEXLabel = new Label("Quiz 4", Label.LEFT);
TextField prelimsEXField = new TextField(10);
Label midtermQ1Label = new Label("Quiz 5", Label.LEFT);
TextField midtermQ1Field = new TextField(10);
Label midtermQ2Label = new Label("Quiz 6", Label.LEFT);
TextField midtermQ2Field = new TextField(10);
Label midtermCSLabel = new Label("Quiz 7", Label.LEFT);
TextField midtermCSField = new TextField(10);
Label midtermEXLabel = new Label("Quiz 8", Label.LEFT);
TextField midtermEXField = new TextField(10);
Label finalsQ1Label = new Label("Quiz 9", Label.LEFT);
TextField finalsQ1Field = new TextField(10);
Label finalsQ2Label = new Label("Quiz 10", Label.LEFT);
TextField finalsQ2Field = new TextField(10);
Label finalsCSLabel = new Label("Quiz 11", Label.LEFT);
TextField finalsCSField = new TextField(10);
Label finalsEXLabel = new Label("Quiz 12", Label.LEFT);
TextField finalsEXField = new TextField(10);
Button computeButton = new Button(" Compute ");
Button clearButton = new Button(" Clear ");
Label gradeLabel = new Label("Grade: ", Label.RIGHT);
TextField gradeField = new TextField(10);
Label statusLabel = new Label("Status: ", Label.RIGHT);
TextField statusField = new TextField("enter the data", 10);
public void init() { // begin init-------------------------------
setBackground(Color.white);
setForeground(Color.red);
// add(titleLabel);
add(prelimsQ1Label);
add(prelimsQ1Field);
prelimsQ1Field.setForeground(Color.red);
add(prelimsQ2Label);
add(prelimsQ2Field);
prelimsQ2Field.setForeground(Color.red);
add(prelimsCSLabel);
add(prelimsCSField);
prelimsCSField.setForeground(Color.red);
add(prelimsEXLabel);
add(prelimsEXField);
prelimsEXField.setForeground(Color.red);
add(midtermQ1Label);
add(midtermQ1Field);
midtermQ1Field.setForeground(Color.red);
add(midtermQ2Label);
add(midtermQ2Field);
midtermQ2Field.setForeground(Color.red);
add(midtermCSLabel);
add(midtermCSField);
midtermCSField.setForeground(Color.red);
add(midtermEXLabel);
add(midtermEXField);
midtermEXField.setForeground(Color.red);
add(finalsQ1Label);
add(finalsQ1Field);
finalsQ1Field.setForeground(Color.red);
add(finalsQ2Label);
add(finalsQ2Field);
finalsQ2Field.setForeground(Color.red);
add(finalsCSLabel);
add(finalsCSField);
finalsCSField.setForeground(Color.red);
add(finalsEXLabel);
add(finalsEXField);
finalsEXField.setForeground(Color.red);
add(computeButton);
computeButton.addActionListener(this);
add(clearButton);
clearButton.addActionListener(this);
add(gradeLabel);
add(gradeField);
gradeField.setForeground(Color.gray);
add(statusLabel);
add(statusField);
statusField.setForeground(Color.blue);
} // end init
public void actionPerformed(ActionEvent yhan) {
// declare object strings
String strprelimsQ1 = new String(prelimsQ1Field.getText());
String strprelimsQ2 = new String(prelimsQ2Field.getText());
String strprelimsCS = new String(prelimsCSField.getText());
String strprelimsEX = new String(prelimsEXField.getText());
String strmidtermQ1 = new String(midtermQ1Field.getText());
String strmidtermQ2 = new String(midtermQ2Field.getText());
String strmidtermCS = new String(midtermCSField.getText());
String strmidtermEX = new String(midtermEXField.getText());
String strfinalsQ1 = new String(finalsQ1Field.getText());
String strfinalsQ2 = new String(finalsQ2Field.getText());
String strfinalsCS = new String(finalsCSField.getText());
String strfinalsEX = new String(finalsEXField.getText());
if (yhan.getSource() == computeButton)
{
if (strprelimsQ1.equals("")) {
prelimsQ1Field.setText("0");
}
if (strprelimsQ2.equals("")) {
prelimsQ2Field.setText("0");
}
if (strprelimsCS.equals("")) {
prelimsCSField.setText("0");
}
if (strprelimsEX.equals("")) {
prelimsEXField.setText("0");
}
if (strmidtermQ1.equals("")) {
midtermQ1Field.setText("0");
}
if (strmidtermQ2.equals("")) {
midtermQ2Field.setText("0");
}
if (strmidtermCS.equals("")) {
midtermCSField.setText("0");
}
if (strmidtermEX.equals("")) {
midtermEXField.setText("0");
}
if (strfinalsQ1.equals("")) {
finalsQ1Field.setText("0");
}
if (strfinalsQ2.equals("")) {
finalsQ2Field.setText("0");
}
if (strfinalsCS.equals("")) {
finalsCSField.setText("0");
}
if (strfinalsEX.equals("")) {
finalsEXField.setText("0");
}
// Converting input to values
int Q1 = Integer.parseInt(prelimsQ1Field.getText());
int Q2 = Integer.parseInt(prelimsQ2Field.getText());
int Q3 = Integer.parseInt(prelimsQ2Field.getText());
int Q4 = Integer.parseInt(prelimsEXField.getText());
int Q5 = Integer.parseInt(midtermQ1Field.getText());
int Q6 = Integer.parseInt(midtermQ2Field.getText());
int Q7 = Integer.parseInt(midtermQ2Field.getText());
int Q8 = Integer.parseInt(midtermEXField.getText());
int Q9 = Integer.parseInt(finalsQ1Field.getText());
int Q10 = Integer.parseInt(finalsQ2Field.getText());
int Q11 = Integer.parseInt(finalsQ2Field.getText());
int Q12 = Integer.parseInt(finalsEXField.getText());
// Calculations
double grade = (Q1 + Q2 + Q3 + Q4
+ Q5 + Q6 + Q7 + Q8 + Q9 + Q10+ Q11+ Q12) / 12;
try {
BufferedWriter out = new BufferedWriter(new FileWriter("Grade output.txt"));
//for (int i = 0; i < 11; i++) {
out.write(Q1 + " ");
System.out.println(Q1);
out.close();
} catch (IOException e) {}
// Output grade
gradeField.setText("" + Math.round(grade));
if (grade < 75) {
statusField.setText("failed");
} else {
statusField.setText("Pass");
}
if (grade > 100) {
statusField.setText("You may have invalid input");
}
} // end if computeButton
if (yhan.getSource() == clearButton) {
gradeField.setText("");
prelimsQ1Field.setText("");
prelimsQ2Field.setText("");
prelimsCSField.setText("");
prelimsEXField.setText("");
midtermQ1Field.setText("");
midtermQ2Field.setText("");
midtermCSField.setText("");
midtermEXField.setText("");
finalsQ1Field.setText("");
finalsQ2Field.setText("");
finalsCSField.setText("");
finalsEXField.setText("");
statusField.setText("reenter the data");
} // end if clearButton
} // end actionperformed
} // 结束类 '