为每个新文件刷新一个计数器++

时间:2013-11-27 00:59:47

标签: java counter

如果我有四个不同的文件,我正在通过count++循环,我将如何刷新count++并从每个新文件的1开始?

现在,文件的输出是:

20    
37  
57  
76

但是我希望它刷新并从1开始,所以输出将是:

20    
17    
20   
16

我的完整代码:

import java.io.File;

import java.io.FileNotFoundException; import java.util.Scanner;

公共班级评分员{

public static int Score = 0;

public static void getFileInfo(String fileName)
        throws FileNotFoundException {


    Scanner in = new Scanner(new File(fileName));

    while (in.hasNext()) { 
    String fileContent = in.nextLine();
    String result = removeSpaces(fileContent);

    double first = Double.parseDouble(fileContent.substring(0, fileContent.indexOf(" ")));
    char operator = getOperator(fileContent);
    double second = secondNumber(result, fileContent);
    double last = Double.parseDouble(result.substring(result.indexOf("=") + 1));

    double math = mathChecking(first, second, operator);

    mathGrading(math , last);

    }
    System.out.println(Score);

}

public static String removeSpaces(String content){
    String result = content.replace(" ","");
    return result;
}

public static double mathGrading(double math , double last) {

    if (math == last){
        Score++;
    }
    return Score;   
}

public static double secondNumber(String result, String opContent){

    int checkAdd = opContent.indexOf('+');
    int checkMinus = opContent.indexOf('-');
    int checkMulti = opContent.indexOf('*');
    int checkDivi = opContent.indexOf('/');

    if (checkAdd != -1){
        return Double.parseDouble(result.substring(result.indexOf('+')+1 , result.indexOf('='))); 
    }   
    else if (checkMinus != -1) {
        return Double.parseDouble(result.substring(result.indexOf('-')+1 , result.indexOf('='))); 
    }
    else if (checkMulti != -1) {
        return Double.parseDouble(result.substring(result.indexOf('*')+1 , result.indexOf('='))); 
    }
    else if (checkDivi != -1){
        return Double.parseDouble(result.substring(result.indexOf('/')+1 , result.indexOf('='))); 
    }
    return 0;
}


public static char getOperator(String fileContent){


    int checkAdd = fileContent.indexOf('+');
    int checkMinus = fileContent.indexOf('-');
    int checkMulti = fileContent.indexOf('*');
    int checkDivi = fileContent.indexOf('/');

    if (checkAdd != -1){
        char operator = fileContent.charAt(fileContent.indexOf('+')); 
        return operator;
    }   
    else if (checkMinus != -1) {
       char operator = fileContent.charAt(fileContent.indexOf('-'));
       return operator;
    }
    else if (checkMulti != -1) {
        char operator = fileContent.charAt(fileContent.indexOf('*'));
        return operator;
    }
    else if (checkDivi != -1){
        char operator = fileContent.charAt(fileContent.indexOf('/'));
        return operator;
    }
    return ' ';
}

public static double mathChecking(double first, double second, char operator){

    double math = 0;
    if (operator == '+'){
        return math = (first + second);
    }
    else if (operator == '-'){
        return math = (first - second);
    }
    else if (operator == '*'){
        return math = (first * second);
    }
    else if (operator == '/'){
        return math = (first / second);
    }

    return math;
}

这是Starter课程:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

  public class Starter {

public static void main(String[] args) throws FileNotFoundException {


    Grader.getFileInfo("data\\studentSubmissionA.txt");
    Grader.getFileInfo("data\\studentSubmissionA2.txt");
    Grader.getFileInfo("data\\studentSubmissionB.txt");
    Grader.getFileInfo("data\\studentSubmissionB2.txt");

1 个答案:

答案 0 :(得分:1)

将getFileInfo方法更改为以下内容:

public static void getFileInfo(String fileName) throws FileNotFoundException {

Scanner in = new Scanner(new File(fileName));
Score = 0;

    while (in.hasNext()) { 
    String fileContent = in.nextLine();
    String result = removeSpaces(fileContent);

    double first = Double.parseDouble(fileContent.substring(0, fileContent.indexOf(" ")));
    char operator = getOperator(fileContent);
    double second = secondNumber(result, fileContent);
    double last = Double.parseDouble(result.substring(result.indexOf("=") + 1));

    double math = mathChecking(first, second, operator);

    mathGrading(math , last);

    }
    System.out.println(Score);

}