2013-03-15 61 views
0

我很確定我有這個想法,但我有3個數組,我初始化不在主要方法,但顯然我不能調用這些數組時調用方法的主要方法,所以我的問題是,用我下面的代碼有沒有辦法讓我的方法來識別它們?或者我能做些什麼來初始化主要方法中的所有內容?我需要有一個閱讀方法一級方法平均方法和輸出方法。嘗試調用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 

回答

0

一個解決方案是使陣列到全局變量在你的程序...這可能是端口的最簡單的想法到解決方案

另一個解決辦法是將返回從數組創建它們的單個函數,然後從主函數中調用這些函數。

這些事情中的任何一個都應該做更多或更少的事情。

+0

此外,這似乎很像功課,這就是爲什麼我試圖不要給你一個完全具體的答案。不過,我希望這足以讓你解決你的問題!祝你好運! – krishnakid 2013-03-15 03:30:02

+0

因此,如果只是返回每個數組,它會在我調用該方法時識別它?是的,這是家庭作業對不起,我只是卡住了,不能看到我需要的東西。 – 2013-03-15 03:40:59

相關問題