第一次使用java社区但我不明白如何格式化代码以便在此处正确显示。将输出导出为记事本文本格式的教程让我很困惑。我将我的代码上传到一个文本文件中供大家查看。
该代码主要用于比较一个真或假类问题数组
package grades;
import java.io.*;
import javax.swing.*;
import java.text.*;
import java.lang.*;
public class Grades {
String[] studentIDArr = new String[8];
String score = "";
String answer1 = "";
String answer2 = "";
String answer3 = "";
String answer4 = "";
String answer5 = "";
String answer6 = "";
String answer7 = "";
String answer8 = "";
String answer9 = "";
String answer10 = "";
int AnswerKey = 0;
String answerKey1 = "";
String answerKey2 = "";
String answerKey3 = "";
String answerKey4 = "";
String answerKey5 = "";
String answerKey6 = "";
String answerKey7 = "";
String answerKey8 = "";
String answerKey9 = "";
String answerKey10 = "";
String Opt = "*****Enter T or F*****";
int pass = 0;
int fail = 0;
int passcounter = 0;
int failcounter = 0;
JOptionPane popup = new JOptionPane();
static int i = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//invoke the constructor method
new Grades();
}
public Grades() {
AnswerKey = Integer.parseInt(popup.showInputDialog("Please enter Answer Key Number"));
if (AnswerKey == 0000) {
answerKey1 = JOptionPane.showInputDialog(Opt);
answerKey2 = JOptionPane.showInputDialog(Opt);
answerKey3 = JOptionPane.showInputDialog(Opt);
answerKey4 = JOptionPane.showInputDialog(Opt);
answerKey5 = JOptionPane.showInputDialog(Opt);
answerKey6 = JOptionPane.showInputDialog(Opt);
answerKey7 = JOptionPane.showInputDialog(Opt);
answerKey8 = JOptionPane.showInputDialog(Opt);
answerKey9 = JOptionPane.showInputDialog(Opt);
answerKey10 = JOptionPane.showInputDialog(Opt);
for (i = 0; i < studentIDArr.length; i++) {
studentIDArr[i] = JOptionPane.showInputDialog("Please enter Student ID");
studentIDArr[i] = studentIDArr[i].replaceAll("[^0-9]", "");
answer1 = JOptionPane.showInputDialog(Opt);
if (answer1.equals(answerKey1)) {
pass = pass + 1;
}
answer2 = popup.showInputDialog(Opt);
if (answer2.equals(answerKey2)) {
pass = pass + 1;
}
answer3 = popup.showInputDialog(Opt);
if (answer3.equals(answerKey3)) {
pass = pass + 1;
}
answer4 = popup.showInputDialog(Opt);
if (answer4.equals(answerKey4)) {
pass = pass + 1;
}
answer5 = popup.showInputDialog(Opt);
if (answer5.equals(answerKey5)) {
pass = pass + 1;
}
answer6 = popup.showInputDialog(Opt);
if (answer6.equals(answerKey6)) {
pass = pass + 1;
}
answer7 = popup.showInputDialog(Opt);
if (answer7.equals(answerKey7)) {
pass = pass + 1;
}
answer8 = popup.showInputDialog(Opt);
if (answer8.equals(answerKey8)) {
pass = pass + 1;
}
answer9 = popup.showInputDialog(Opt);
if (answer9.equals(answerKey9)) {
pass = pass + 1;
}
answer10 = popup.showInputDialog(Opt);
if (answer10.equals(answerKey10)) {
pass = pass + 1;
}
if (pass >= 0 && pass < 4) {
score = "F";
failcounter++;
} else if (pass >= 4 && pass < 6) {
score = "C";
passcounter++;
} else if (pass >= 6 && pass < 8) {
score = "B";
passcounter++;
} else if (pass >= 8 && pass < 11) {
score = "A";
passcounter++;
}
fail = 10 - pass;
System.out.println("Student ID: " + studentIDArr[i]);
System.out.println("Student Score: " + score);
System.out.println("The Student passed " + pass + " questions");
System.out.println("The Student failed " + fail + " questions");
}
System.out.println("Number of Students Passed: " + passcounter);
System.out.println("Number of Students Failed: " + failcounter);
} else {
JOptionPane.showMessageDialog(null, "Wrong Number", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
}
答案 0 :(得分:0)
我认为您想要使用打印行所在的java.io.BufferedWriter
。
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();