Cố gắng gọi các phương thức trong phương thức main với biến được khởi tạo trong các phương thức khác (Trying to call methods in main method with variable initialized in other methods)


問題描述

Cố gắng gọi các phương thức trong phương thức main với biến được khởi tạo trong các phương thức khác (Trying to call methods in main method with variable initialized in other methods)

I am pretty sure i have this all figured out but i have 3 arrays that i initialized not in the main method, but obviously i cant call those arrays when calling the method in the main method, so my question is, with the code i have below is there any way for me to get the methods to recognize them? or what can i do to initialize everything in the main method? i need to have a read in method a grade method an average method and an output method.

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:

one solution would be to make the arrays into global variables in your program... and this might be the simplest idea to port into your solution

another solution would be to return the arrays from the individual functions that are creating them and then call those functions from the main function.  

either of these things should do more or less what you want.

(by Joseph Mindrupkrishnakid)

參考文件

  1. Trying to call methods in main method with variable initialized in other methods (CC BY‑SA 3.0/4.0)

#split #static-methods #java #methods #arrays






相關問題

將 xml 元素內容拆分為固定行數 (Split xml element content into fix number of lines)

是否有任何標准說明“aba”.split(/a/) 是否應該返回 1,2 或 3 個元素? (Is there any standard which says if "aba".split(/a/) should return 1,2, or 3 elements?)

Cố gắng gọi các phương thức trong phương thức main với biến được khởi tạo trong các phương thức khác (Trying to call methods in main method with variable initialized in other methods)

使用 Java-Regex 與 Regex 成對拆分多行文本 (Split text with Java-Regex in pairs with Regex over several lines)

如何分割字節數組 (How to split a byte array)

String componentsSeparatedByString 做一次 (String componentsSeparatedByString do one time)

從一行文本中獲取特定數據 (Get specific data from a line of text)

(Python)拆分字符串多個分隔符更有效?1) 使用多重替換方法然後使用拆分 2) 使用正則表達式 ((Python) which is more efficient to split a string multiple separators? 1) Using multiple replace method then using split 2) using regular Expressions)

ValueError:發現樣本數量不一致的輸入變量:[2935848、2935849] (ValueError: Found input variables with inconsistent numbers of samples: [2935848, 2935849])

在 Powershell 中拆分和添加字符串 (Splitting and Adding String in Powershell)

在 python 函數中檢查月份的有效性時出錯 (Error in checking validity of month in python function)

如何將 .obj 文件拆分為其他兩個文件(python、open3d)? (How to split a .obj file into two other files (python, open3d)?)







留言討論