数组列表计数用户 [英] Array List count to users

查看:126
本文介绍了数组列表计数用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个类,做不同的事情。我想用户的循环计数在数组列表中的用户数。基本上类是获取信息并添加关于学生的信息。一个被输入的东西是采取由学生信用的数量。让我们说,我进5名学生到我的数组列表。两名学生正在5学分2名学生正在服用6个学分,最后一个学生正在9学分。我创建了一些code类中,让说,用户想知道有多少学生在数组中正在服用6学分。所以,我创建了一个环节,让用户输入号码和类看起来数组中,并返回多少学生正在这个数字,但它不工作。我不知道这是有道理的。

  System.out.print(请输入一个数字学分:\\ n);
inputInfo = stdin.readLine()修剪()。
诠释学分=的Integer.parseInt(inputInfo);诠释计数= 0;
的for(int i = 0; I< studentList.size(); ++ I)
{
  算上++;
}
的System.out.println(+信贷谁正在学生人数
                               +信用是:+计数);打破;


解决方案

有关每一个学生,你必须检查他是否有您正在寻找的学分相同。

通过替换您的循环:

  / *如果你的列表中包含学生对象的数组* /
对于(学生学生:studentList){
   如果(student.getCredits()==学分){
       算上++;
   }
}


  / *如果你不使用对象* /
的for(int i = 0; I< studentList.size();我++){
   如果(studentList [I] .credits ==学分){
       算上++;
   }
}

I wrote a class that does different things. I am trying to user a loop to count the number of users in an array list. Basically the class is taking information and adding that info regarding students. One of the things being entered is the number of credits being taken by students. Let say I entered 5 students into my array list. two students are taking 5 credits 2 students are taking 6 credits and the last student is taking 9 credits. I created some code in the class, let say that the user wants to know how many students in the array are taking 6 credits. So I created a segment that lets the user enter that number and the class would look in the array and return how many students are taking that number, but its not working. I dont know if this makes sense

System.out.print("Please enter a number of credits:\n");
inputInfo = stdin.readLine().trim();
int credits = Integer.parseInt(inputInfo);

int count = 0;


for (int i = 0; i < studentList.size(); ++i)
{
  count++;
}   


System.out.println("The number of students who are taking " + credits
                               + " credits is: " + count);

break; 

解决方案

For each student, you have to check whether he has the same amount of credits that you are looking for.

Replace your for loop by:

/* If your list contains an array of Student objects */
for(Student student : studentList) {
   if (student.getCredits() == credits) {
       count++;
   }
}


/* If you don't use objects */
for(int i = 0; i < studentList.size(); i++) {
   if(studentList[i].credits == credits) {
       count++;
   }
}

这篇关于数组列表计数用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆