【问题标题】:Finding the number of fails between 3 sets of array查找 3 组数组之间的失败次数
【发布时间】:2021-03-18 22:01:21
【问题描述】:

这是一个简单的编码问题,要求对每个学生的分数求和。如果总分达到及格分数,则到达下一个学生。如果他们没有达到及格分数,那么它将返回失败。

class StudentMarks {

public int getCountFailures() {
    int[] student1 = {3, 2, 6, 4, 3, 6, 6, 7, 3, 2};
    int[] student2 = {8, 7, 8, 9, 10, 7, 6, 8, 9, 6};
    int[] student3 = {2, 5, 3, 1, 4, 3, 3, 2, 5, 6};
    int[][] allStudents = {student1, student2, student3};
    int numberFails = 0;
    int passMark = 50;

    // YOUR CODE GOES HERE
   



    return numberFails;
}

}

测试代码

    int numberFails = marks.getCountFailures();
    System.out.println("Number of fails = " + numberFails);

我将如何实现一种方法来汇总每个学生的分数并返回失败的数量。

预期结果

失败次数 = 2

【问题讨论】:

标签: java arrays arraylist


【解决方案1】:

似乎是一个家庭作业问题,所以这不是一个完整的答案,但会帮助你自己想出一个。

// will return the count of the arrays - the sum of the elements
// of which are equal to or cross the threshold.
long count = 
    Arrays.stream(allStudents)
        .peek(a -> System.out.println(Arrays.toString(a)))
        .filter(a -> thresholdSum(a) )
        .count();
    
System.out.print(count);

这是辅助方法。

public static boolean thresholdSum(int[] a) {
    
    int threshold = 50;
    int sum =0;
    for (int i : a) {
        sum += i;
        if (sum >= threshold) {
            return true;
        }
        
    }
    return false;
    
    
}

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    相关资源
    最近更新 更多