数组没有返回值

时间:2016-08-22 10:30:04

标签: java arrays

嗨第一次海报和java初学者。

问题:尝试使用 gradeDistn 方法返回学生分数的计数值

gradeDistn方法将接受一个字符数组,这些字符是为标记数组指定的等级,例如grade方法返回的等级。 gradeDistn方法将返回一个整数值数组,其中包含成绩分布,即所分配成绩中每个成绩的出现次数。用于成绩的字符是固定的。返回的数组应按照从A级到F级的顺序提供分布

输出

Min是24

Max是97

范围是74

平均值为62

中位数为62

模式是53

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEEEEEDDDDDDDDDDCCCCCCCCCCCCCCCBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

商标的分配

A:1

B:1

C:1

D:1

E:1

F:1

import java.util.Random;
import java.lang.Math;
import java.lang.reflect.Array;
import java.util.Arrays;



    public class Marks {

private static final int NMARKS = 125;
private static final double mean = 65.0;
private static final double std = 15.0;

/**
 * Returns an array of NMARKS integer marks approximately normally distributed,
 * with specified mean (mean) and standard deviation (std),
 * and with values outside 0..100 removed.
 * @return the array of marks.
 */
public static int[] getMarks() {
    Random rand = new Random(1001L);
    int mark;
    int[] theMarks = new int[NMARKS];
    int n = 0;
    while (n < NMARKS) {
        mark = (int) Math.round(std*rand.nextGaussian() + mean);
        if (mark >= 0 && mark <= 100)
            theMarks[n++] = mark;
    }
    return theMarks;
}

/**
 * Test code
 * @param args not used
 */
public static void main(String[] args) {

    int[] testMarks = getMarks();


    int[] sortedMarks = arraySort (testMarks);
    System.out.println("Unsorted marks\n");
    for (int n = 0; n < testMarks.length; n++) {
        System.out.print(testMarks[n] + " ");
        if (n % 10 == 9)
            System.out.println();
    }

    System.out.println("\n\nSorted marks\n");

    for (int n = 0; n < sortedMarks.length; n++) {
        System.out.print(sortedMarks[n] + " ");
        if (n % 10 == 9)
            System.out.println();

    }
    System.out.println("\n\n");

    //Print Minimum
    int minimum = getMin(getMarks());
    System.out.println("Min is " + minimum);

    //Print Maximum
    int maximum = getMax(getMarks());
    System.out.println("Max is " + maximum);

    //Print Range
    int range = (getRange(maximum, minimum));
    System.out.println("Range is " + range);

    //Print Mean
    int mean = getMean(getMarks());
    System.out.println("Mean is " + mean);

    //Print Median
    int median = getMedian(sortedMarks);
    System.out.println("Median is " + median);

    //Print Mode
    int mode = getMode(sortedMarks);
    System.out.println("Mode is " + mode);

    //Print Grades
    char[] grades = getGrades(sortedMarks);
    System.out.println(grades);
    //Print gradeDistn

    int[] gradeDistrubution = gradeDistn(grades);

    System.out.println("Distribution of Marks\nA: " + gradeDistrubution[0] + "\nB: " + gradeDistrubution[1] + "\nC: " + 
            gradeDistrubution[2] + "\nD: " + gradeDistrubution[3] + "\nE: " + gradeDistrubution[4] + 
            "\nF: " + gradeDistrubution[5]);


}

//getMin method will return the minimum mark
public static int getMin(int[] inputArray){
    int minValue = inputArray[0];
    for(int i=1;i < inputArray.length;i++){
        if(inputArray[i] <minValue){
            minValue = inputArray[i];
        }
    }
    return minValue;

}   
//getMax method will return the maximum mark
public static int getMax(int[] inputArray){
    int maxValue = inputArray[0];
    for(int i=1;i < inputArray.length;i++){
        if(inputArray[i] > maxValue){
            maxValue = inputArray[i];
        }
    }
    return maxValue;

}
//getRange method will find the difference between the maximum and minimum marks
public static int getRange(int maxNum, int minNum){
    int max = maxNum;
    int min = minNum;
    int range = (max - min) + 1;
    return range;

}

//getMean method will return the average of marks
public static int getMean(int[] inputArray){
    int total = 0;
    for(int i: inputArray){
        total += i;
    }
    return total/(inputArray.length);

}

// arraySort method sorts the array of grades in numerical ascending order
public static int [] arraySort(int [] inputArray){
    int [] sortedArray = Arrays.copyOf(inputArray, inputArray.length); 
    Arrays.sort(sortedArray);
    return sortedArray;


}
//getMedian method will return the median value of the set of marks
public static int getMedian(int[] inputArray){
    int listLength = inputArray.length;
    int x = listLength / 2;
    int median = inputArray[x];

    return median; 

}

//getMode method will return the mode of the set of marks, which is the most commonly occurring mark
public static int getMode(int[] n) {
    int maxValue = 0;
    int maxCounts = 0;
    int[] counts = new int[n.length];

    for (int i=0; i < n.length; i++) {
        counts[n[i]]++;
        if (maxCounts < counts[n[i]]) {
            maxCounts = counts[n[i]];
            maxValue = n[i];
        }
    }
    return maxValue;
}

/*grades method will return an array of characters which are the grades 
  corresponding to the integer marks in the array of marks */
public static char [] getGrades(int[] n){
    int[] grades = new int[n.length]; 
    char[] marks = {'A', 'B', 'C', 'D', 'E', 'F'};
    char[] convertedGrades = new char[n.length];


    for (int i = 0 ; i < grades.length; i++){
        grades [i] = i;
        if (grades[i] >= 90)                                        
            convertedGrades[i] = marks[0];
        else if (grades[i] >= 75)                                       
            convertedGrades[i] = marks[1];
        else if (grades[i] >= 60)                                       
            convertedGrades[i] = marks[2];
        else if (grades[i] >= 50)                                       
            convertedGrades[i] = marks[3];
        else if (grades[i] >= 45)                                       
            convertedGrades[i] = marks[4];
        else                                    
            convertedGrades[i] = marks[5];
    }
     return convertedGrades; 
}

// GRADESDISTN METHOD
public static int[] gradeDistn(char[] arr){
    char[] characterArray = new char[arr.length]; 
    int A=0,B=0,C=0,D=0,E=0,F=0;

    for (char i = 0 ; i < characterArray.length; i++){
        characterArray [i] = i;
        if (characterArray[i] == 'A')
            A++;
        if (characterArray[i] == 'B')
            B++;
        if (characterArray[i] == 'C')
            C++;
        if (characterArray[i] == 'D')
            D++;
        if (characterArray[i] == 'E')
            E++;
        if (characterArray[i] == 'F')
            F++;
    }
     int[] marks = {A, B, C, D, E, F};
     return marks; 
}

}

2 个答案:

答案 0 :(得分:1)

 int A=0,B=0,C=0,D=0,E=0,F=0;
 int[] marks = {A, B, C, D, E, F};

不会将marksAB等联系起来。 增加A marks时保持不变。

我建议你在完成所有计算后构建marks。只需在返回前放置int[] marks = {A, B, C, D, E, F};

PS:if (characterArray[i] == 'B') A++;对我来说不合适。

答案 1 :(得分:0)

由于@talex解释了您的代码有什么问题,我可以为您指出一个更好的解决方案:因为您的grades是字母序列,所以它们可以匹配长度为'F' - 'A' + 1的数组(数量为'A''F'之间的字母。匹配函数将尽可能简单:使用grade - 'A'作为数组的索引,使用i + 'A'作为恢复函数。