java - 使用循环检查数组中的每个元素并返回 true 或 false
问题描述
我正在尝试编写一个循环来检查包含测试分数的数组中的每个元素。如果测试分数达到阈值,那么它应该在该位置返回真或假。这是到目前为止的代码。我觉得我走在正确的轨道上,并且了解应该如何完成,但对 Java 的了解还不够多。
public class SimpleArray {
/**
* Write a function that takes in an applicant's numerical scores and returns
* Boolean values to tell us if they are above a certain threshold.
*
* For example, if the applicant's scores are [80, 85, 89, 92, 76, 81], and the
* threshold value is 85, return [false, false, true, true, false, false].
*
* @param scores The applicant's array of scores
* @param threshold The threshold value
* @return An array of boolean values: true if the score is higher than
* threshold, false otherwise
*/
public static boolean[] applicantAcceptable(int[] scores, int threshold) {
boolean[] highScores = new boolean[scores.length];
/*
* TO DO: The output array, highScores, should hold as its elements the
* appropriate boolean (true or false) value.
*
* Write a loop to compute the acceptability of the scores based on the
* threshold and place the result into the output array.
*/
for (int i = 0; i < highScores.length; i++){
if (highScores[i] <= threshold[i]);
return highScores;
}
}
解决方案
只需将值分配scores[i] > threshold
给您的布尔数组:
boolean[] highScores = new boolean[scores.length];
for (int i = 0; i < highScores.length; i++)
highScores[i] = scores[i] > threshold;
推荐阅读
- highcharts - 具有可变 x 点宽度的堆叠分组柱形图
- excel - 按总和和排序文件中包含的多个工作簿分组
- javascript - 使用 if 时设置所有语句
- javascript - reCAPTCHA 占位符错误,因为页面上没有验证码
- c++ - Visual Studio(或任何其他工具)可以将地址解释为调用堆栈(增强上下文)的开头吗?
- r - 带有十进制格式日期的csv
- python - Python 如何从 Double 中分辨出 Int
- install4j - 如何使用 install4j 静默安装程序添加开始菜单快捷方式
- laravel - Laravel/lumen tymon/jwt-auth 从提供第一个用户记录的 JWT 令牌中获取数据
- php - PHP session gc_maxlifetime 使用主值而不是本地值