* 定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
* 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定,打印出3年级(state值为3)的学生信息。
*
*/
import java.util.ArrayList;
import java.util.List;
public class StudentManager {
public static void main(String[] args){
List<Student> list = new ArrayList<Student>(); // 容器List, 泛型
for(int i=0; i<20; i++){
Student student = new Student();
student.setNumber(i+1);
int state = (int)(Math.random()*3+1);
//System.out.println("年级="+state);
student.setState(state);
student.setScore((int)(Math.random()*100));
list.add(student);
}
//将List中的数据 挨个拿出来赋值给每个student对象, 下面这段增强for循环改成普通for循环后,语句块内如何改动?
for (Student student : list) {
if(student.getState() <= 3 && student.getState() !=0){
if(student.getScore() >= 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---合格!");
}else if(student.getScore() < 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore());
System.out.println("---不合格!");
}
}else{
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
}
}
}
class Student {
private int number;
private int state;
private int score;
public Student() { //默认构造方法
}
public Student(int number, int state, int score) { //构造方法重载
this.number = number;
this.state = state;
this.score = score;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
3 个解决方案
#1
这里如何改成普通的for循环语句?
for (Student student : list) {
if(student.getState() <= 3 && student.getState() !=0){
if(student.getScore() >= 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---合格!");
}else if(student.getScore() < 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore());
System.out.println("---不合格!");
}
}else{
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
for (Student student : list) {
if(student.getState() <= 3 && student.getState() !=0){
if(student.getScore() >= 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---合格!");
}else if(student.getScore() < 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore());
System.out.println("---不合格!");
}
}else{
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
#2
if (null != list && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getState() <= 3 && student.getState() != 0) {
if (student.getScore() >= 60) {
System.out.print("年级:" + student.getState()
+ " 学号:" + student.getNumber() + " 分数"
+ student.getScore() + " ");
System.out.println("---合格!");
} else if (student.getScore() < 60) {
System.out.print("年级:" + student.getState()
+ " 学号:" + student.getNumber() + " 分数"
+ student.getScore());
System.out.println("---不合格!");
}
} else {
System.out.print("年级:" + student.getState() + " 学号:"
+ student.getNumber() + " 分数"
+ student.getScore() + " ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
}
}
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getState() <= 3 && student.getState() != 0) {
if (student.getScore() >= 60) {
System.out.print("年级:" + student.getState()
+ " 学号:" + student.getNumber() + " 分数"
+ student.getScore() + " ");
System.out.println("---合格!");
} else if (student.getScore() < 60) {
System.out.print("年级:" + student.getState()
+ " 学号:" + student.getNumber() + " 分数"
+ student.getScore());
System.out.println("---不合格!");
}
} else {
System.out.print("年级:" + student.getState() + " 学号:"
+ student.getNumber() + " 分数"
+ student.getScore() + " ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
}
}
#3
Student student = list.get(i); // E get(int index) Returns the element at the specified position in this list.
非常感谢!!!
非常感谢!!!
#1
这里如何改成普通的for循环语句?
for (Student student : list) {
if(student.getState() <= 3 && student.getState() !=0){
if(student.getScore() >= 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---合格!");
}else if(student.getScore() < 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore());
System.out.println("---不合格!");
}
}else{
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
for (Student student : list) {
if(student.getState() <= 3 && student.getState() !=0){
if(student.getScore() >= 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---合格!");
}else if(student.getScore() < 60) {
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore());
System.out.println("---不合格!");
}
}else{
System.out.print("年级:"+student.getState()+" 学号:"+student.getNumber()+" 分数"+student.getScore()+" ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
#2
if (null != list && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getState() <= 3 && student.getState() != 0) {
if (student.getScore() >= 60) {
System.out.print("年级:" + student.getState()
+ " 学号:" + student.getNumber() + " 分数"
+ student.getScore() + " ");
System.out.println("---合格!");
} else if (student.getScore() < 60) {
System.out.print("年级:" + student.getState()
+ " 学号:" + student.getNumber() + " 分数"
+ student.getScore());
System.out.println("---不合格!");
}
} else {
System.out.print("年级:" + student.getState() + " 学号:"
+ student.getNumber() + " 分数"
+ student.getScore() + " ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
}
}
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getState() <= 3 && student.getState() != 0) {
if (student.getScore() >= 60) {
System.out.print("年级:" + student.getState()
+ " 学号:" + student.getNumber() + " 分数"
+ student.getScore() + " ");
System.out.println("---合格!");
} else if (student.getScore() < 60) {
System.out.print("年级:" + student.getState()
+ " 学号:" + student.getNumber() + " 分数"
+ student.getScore());
System.out.println("---不合格!");
}
} else {
System.out.print("年级:" + student.getState() + " 学号:"
+ student.getNumber() + " 分数"
+ student.getScore() + " ");
System.out.println("---不好意思,不存在3年级以上的学生!");
}
}
}
#3
Student student = list.get(i); // E get(int index) Returns the element at the specified position in this list.
非常感谢!!!
非常感谢!!!