如何从文件和打印结果计算到File I / O java中的另一个文件

时间:2015-12-14 15:18:41

标签: java file io

我有这个任务: 文件输入得分.txt包含六个测验的基本数字和标记。每个测验的最大标记为15.编写一个程序,从输入文件中读取矩阵编号和标记。计算每个学生的总数和平均值并写入输出文件。分数由空格分隔。使用计算的平均分,将其转换为百分比,并根据UTM分级方案设置分数。

image of tables of inputs and outputs

我做这个编码,但我有问题,当我按下运行有错误

import java.io.PrintWriter;
import java.util.*;
public class Q1 {
public static int number,number1,number2;
public static double sum,sum1,sum2,average,average1,average2;

public static void main(String[] args)throws Exception {
    // TODO Auto-generated method stub
    java.io.File file=new java.io.File("input.txt");
    Scanner input=new Scanner(file);
    while(input.hasNext()){

        number=input.nextInt();
        int q1=input.nextInt();
        int q2=input.nextInt();
        int q3=input.nextInt();
        int q4=input.nextInt();
        int q5=input.nextInt();
        sum=q1+q2+q3+q4+q5;
        average=sum/5;
        number1=input.nextInt();
        int q21=input.nextInt();
        int q22=input.nextInt();
        int q23=input.nextInt();
        int q24=input.nextInt();
        int q25=input.nextInt();
        sum1=q21+q22+q23+q24+q25;
        average1=sum/5;
        number2=input.nextInt();
        int q31=input.nextInt();
        int q32=input.nextInt();
        int q33=input.nextInt();
        int q34=input.nextInt();
        int q35=input.nextInt();
        sum2=q31+q32+q33+q34+q35;
        average2=sum/5;

        input.close();
    }
       PrintWriter output=new java.io.PrintWriter("file1.txt");
       output.print(number);
       output.print(sum);
       output.println(average);
       output.print(number1);
       output.print(sum1);
       output.println(average1);
       output.print(number2);
       output.print(sum2);
       output.println(average2);
       output.close();
       PrintWriter output1=new java.io.PrintWriter("file2.txt");
       output.print(number);
       output.print(sum);
       output.println(check(average));
       output.print(number1);
       output.print(sum1);
       output.println(check(average1));
       output.print(number2);
       output.print(sum2);
       output.println(check(average2));
       output.close();
}
public static double check(double a){

    if(a>=80){
        System.out.print("A");


    }
    else if((a<80)&&(a>=70)){
        System.out.print("B");

        }
    else if((a>=60)&&(a<70)){
        System.out.print("C");
    }
    else 
        System.out.print("D");
    return a;

}

}

1 个答案:

答案 0 :(得分:0)

您的输出确实有错字:

PrintWriter output1=new java.io.PrintWriter("file2.txt");
output.print(number);
output.print(sum);
output.println(check(average));
output.print(number1);
output.print(sum1);
output.println(check(average1));
output.print(number2);
output.print(sum2);
output.println(check(average2));
output.close();

您忘记将其更改为output1