2015-05-25 113 views
0

首次使用java社區,但我不明白如何格式化代碼才能在此處正確顯示。將輸出導出爲記事本文本格式的教程讓我感到困惑。我將我的代碼上傳到了一個文本文件中,供大家看到。將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); 
     } 

    } 
} 
+2

請在這裏用IDE的基本格式發佈相關代碼。本網站中的其他人可以爲您設置格式,然後您將學習如何操作。 –

+0

我做了它我認爲 –

回答

0

我覺得你是想用java.io.BufferedWriter在打印線的位置。

FileWriter fw = new FileWriter(file.getAbsoluteFile());   
BufferedWriter bw = new BufferedWriter(fw);  
bw.write(content); 
bw.close(); 
+0

哪一行我執行此操作。 Java真的讓我困惑? –

+0

@StefanHex'File file = new File(studentIDArr [i] .toString()+「。txt」);' 'FileWriter fw = new FileWriter(file.getAbsoluteFile());' 'BufferedWriter bw = new BufferedWriter FW); ' 'bw.write(「Student ID:」+ studentIDArr [i] +「\ n」);' 'bw.write(「Student Score:」+ score +「\ n」); 'bw.write(「學生通過」+通過+「問題\ n」); bw.write(「學生失敗」+失敗+「問題\ n」); bw.close();' – SparkleGoat

+0

@StefanHex爲您做了這項工作?你可以選擇它作爲答案:D – SparkleGoat