Java ArrayList:使用ArrayList打印消息
在我的Java程序中,我有一个ArrayList。我想做的是在底部打印一个数字,上面写着“x数量的人通过了”Java ArrayList:使用ArrayList打印消息,java,arraylist,Java,Arraylist,在我的Java程序中,我有一个ArrayList。我想做的是在底部打印一个数字,上面写着“x数量的人通过了” System.out.println = ("The amount of people that have more than 40 marks is " + x); 如果使用ArrayList输入的分数数量不确定,是否可以计算出超过40的分数数量 public class test { /** * @param args the command line arguments *
System.out.println = ("The amount of people that have more than 40 marks is " + x);
如果使用ArrayList输入的分数数量不确定,是否可以计算出超过40的分数数量
public class test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Integer> marks = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
// Create a new scanner to use in java
int[] range = { 0,29,39,69,100 };
// A new array is created with the grade boundaries
int[] inRange = new int[boundary.length - 1];
// Indexed from 0 to n-1
int markIn;
// New integer markIn
do {
// This do-while loop calculates the expression after the statements below are exectued at least once
System.out.println("Enter Mark(s):");
// Wait for user input
markIn = input.nextInt();
// markInp value is set as the value entered by user
marks.add(markIn);
for (int a=1 ; a<boundary.length ; a++)
// for loop will take the variable 'a' and compare it with varibale 'boundary', when the condition is satisfied that value of 'a' increments by 1
if (range[a-1] <= markInp && markInp <= range[a]) {
// The boundaries will define the upper and lower limits of the markInp
inRange[a-1]++;
//inRange is incremented by 1
break;
//Exit if
}
} while (markIn <= 100);
// When the mark exceeds 100, the loop is stopped
System.out.println(marks);
input.close();
} // Close the Scanner input
}
如果数组已经排序,如您在示例中所示,那么您只需从开始看到特定分数的位置进行简单搜索,然后获取数组的长度并减去搜索得到的元素的位置
如果数组未排序,请对其排序,然后执行搜索。您可以执行以下操作:
int result = 0;
if(marks != null)
{
Collections.sort(marks);
for(int i=0;i<marks.size();i++)
{
if(marks.get(i) > 40)
{
result = marks.size() - i;
break;
}
}
}
marks是数组列表,result是期望的输出。那么问题到底是什么呢?我的问题是;如果使用ArrayList输入的标记数量不确定,是否可以计算出超过40个的标记数量?该数组可以通过控制台输入任意数量的标记,是否无法将数组与以下内容进行比较:例如,如果数组中的任何元素大于39且小于100???@nMeta_001在输入之前边界是否已知?如果是这样,那么只要在生成输入时遍历并将输入添加到相应的列表中,就可以使用集合而不是数组进行比较。