2015-05-25 66 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