尝试使用其他方法初始化的变量调用main方法中的方法

时间:2013-03-15 03:23:11

标签: java arrays methods split static-methods

我很确定我已经弄明白但是我有3个数组,我初始化不在main方法中,但显然我不能在主方法中调用方法时调用这些数组,所以我的问题是,我在下面的代码是否有任何方法让我获得识别它们的方法?或者我该怎么做才能初始化main方法中的所有内容?我需要读取方法,等级方法,平均方法和输出方法。

import java.util.*;
import java.io.*;

public class Proj5 {
public static void main(String[] args)throws IOException{
    int lines=0;
    int [] quizKey = {1,1,2,2,1,1,3,2,4,1,3,5,4,1,2};
    String [] userAnswers = new String[100];
    int [][] userIndividualAnswers = new int[quizKey.length][userAnswers.length];
    int [] numCorrect = new int[quizKey.length];

        readInText(lines, userAnswers, quizKey);
        displayOutput(lines, percentCorrect, quizKey, userAnswers, userAnswersInt, numCorrect, grade);

}//end main

public static void readInText(int lines, String userAnswers[], int quizKey[])throws IOException{
    Scanner inFile = new Scanner(new File("QuizScores.txt"));
        while(inFile.hasNext()){
            String line = inFile.nextLine();
            String[] tokens = line.split(",");
            userAnswers[lines] = tokens[1];
            lines ++;


        }// end while loop
    int[][] userAnswersInt = new int[quizKey.length][lines];
    char[] grade = new char[lines];
    double[] percentCorrect = new double[lines];
}// end readInText


public static void gradeSingleQuiz(String tokens[], int lines, int quizKey[], String userAnswers[], int numCorrect[], int userAnswersInt[][], char grade[], double percentCorrect[]){
    for (int j=0; j<=lines; j++){
    numCorrect[j]=0;    
        for (int n=0; n<=quizKey.length; n++){
            userAnswersInt[n][j] = Integer.parseInt(userAnswers[n]);
                if(userAnswersInt[n][j]==(quizKey[n])){
                    numCorrect[j]++;    
            }
                if(numCorrect[j]>=14)

grade[j]='A';
                else if((numCorrect[j]>=12)&&(numCorrect[j]<14))
                    grade[j]='B';
                else if((numCorrect[j]>=11)&&(numCorrect[j]<12))
                    grade[j]='C';
                else if ((numCorrect[j]>=9)&&(numCorrect[j]<11))
                    grade[j]='D';
                else
                    grade[j]='F';

            percentCorrect[j] = numCorrect[j]/quizKey.length;

        }//end for loop
    }//end for loop
    for(int i=0; i<lines; i++){
        System.out.println(tokens[0] + "    " + numCorrect[i] + "    " +
                (percentCorrect[i]) + "    " + grade[i]);   

}
}// end gradeSingleQuiz

public static void averageScore(int lines, double percentCorrect[]){
    for(int d=0; d<=lines; d++){    
        System.out.println("Average: " + percentCorrect[d]);
    }//end for loop
}// end averageScore

public static void displayOutput(String tokens[], int lines, int quizKey[], String userAnswers[], int numCorrect[], int userAnswersInt[][], char grade[], double percentCorrect[]){
    System.out.println("Student ID    # Correct    %Correct    Grade");
        gradeSingleQuiz(tokens, lines, quizKey, userAnswers, numCorrect, userAnswersInt, grade, percentCorrect);
}// end display output

}//end class

1 个答案:

答案 0 :(得分:0)

一个解决方案是将数组转换为程序中的全局变量......这可能是移植到解决方案中最简单的想法

另一个解决方案是从创建它们的各个函数返回数组,然后从main函数调用这些函数。

这些事情中的任何一个都应该或多或少地做你想要的事情。