[英]Exporting java output to text
第一次使用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);
}
}
}
我認為您想使用打印行所在的java.io.BufferedWriter
。
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.