展开全部
源代码:
import java.util.*;
public class Test1 {
public static class Student{
int id;
String name;
int math;
int english;
int history;
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setMath(int math){
this.math=math;
}
public int getMath(){
return math;
}
public void setEnglish(int english){
this.english=english;
}
public int getEglish(){
return english;
}
public void setHistory(int history){
this.history=history;
}
public int getHistory(){
return history;
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("请输入学生人数:");
int renshu=scan.nextInt();
Student[]stu=new Student[renshu];
for(int i=0;i
stu[i]=new Student();
int id,math,english,history;
String name;
System.out.println("请输入学生的学号:");
Scanner scan1=new Scanner(System.in);
id=scan1.nextInt();
System.out.println("请输入该生的姓名:");
Scanner scan2=new Scanner(System.in);
name=scan2.next();
System.out.println("请输入该生的数学、英语e68a84e8a2ad62616964757a686964616f31333337626231、历史成绩:");
Scanner scan3=new Scanner(System.in);
math=scan3.nextInt();
english=scan3.nextInt();
history=scan3.nextInt();
stu[i].setId(id);
stu[i].setName(name);
stu[i].setMath(math);
stu[i].setEnglish(english);
stu[i].setHistory(history);
}
Listlist=new ArrayList<>();
list=Arrays.asList(stu);
Iteratorit=list.iterator();
while(it.hasNext()){
Student s=new Student();
s=it.next();
System.out.print("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
System.out.println();
}
System.out.println("三门全部通过的同学为:");
Iteratorit1=list.iterator();
while(it1.hasNext()){
Student s=new Student();
s=it1.next();
if(s.getMath()>=60 && s.getEglish()>=60 && s.getHistory()>=60)
System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
}
System.out.println("三门全部不通过的同学为:");
Iteratorit2=list.iterator();
while(it2.hasNext()){
Student s=it2.next();
if(s.getMath()<60 && s.getEglish()<60 && s.getHistory()<60)
System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
}
System.out.println();
System.out.println("三门至少有一门通过的同学为:");
Iteratorit3=list.iterator();
while(it3.hasNext()){
Student s=it3.next();
if(s.getMath()>=60 || s.getEglish()>60 || s.getHistory()>60)
System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
}
System.out.println();
System.out.println("数学不通过的同学为:");
Iteratorit4=list.iterator();
while(it4.hasNext()){
Student s=it4.next();
if(s.getMath()<60)
System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
}
}
}
其实这样get、set不如直接定义一个构造函数方便。
还有下面这个源代码可能不符合要求,没有键盘输入,但是有分别用for和迭代器,更好理解。
import java.util.*;
public class Test {
public static class Student{
int id;
String name;
int math;
int english;
int history;
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setMath(int math){
this.math=math;
}
public int getMath(){
return math;
}
public void setEnglish(int english){
this.english=english;
}
public int getEglish(){
return english;
}
public void setHistory(int history){
this.history=history;
}
public int getHistory(){
return history;
}
}
public static void main(String[] args) {
Student s1=new Student();
s1.setId(11);
s1.setName("dr");
s1.setMath(100);
s1.setEnglish(90);
s1.setHistory(80);
Student s2=new Student();
s2.setId(12);
s2.setName("spe");
s2.setMath(100);
s2.setEnglish(50);
s2.setHistory(80);
Student s3=new Student();
s3.setId(13);
s3.setName("wr");
s3.setMath(50);
s3.setEnglish(100);
s3.setHistory(80);
Student s4=new Student();
s4.setId(14);
s4.setName("axe");
s4.setMath(50);
s4.setEnglish(50);
s4.setHistory(50);
Listlist=new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
System.out.println("使用迭代器:");
System.out.println("所有学生成绩:");
Iteratorit=list.iterator();
while(it.hasNext()){
Student s=it.next();
System.out.print("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
System.out.println();
}
System.out.println();
System.out.println("三门全部通过的同学为:");
Iteratorit1=list.iterator();
while(it1.hasNext()){
Student s=it1.next();
if(s.getMath()>=60 && s.getEglish()>=60 && s.getHistory()>=60)
System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
}
System.out.println("三门全部不通过的同学为:");
Iteratorit2=list.iterator();
while(it2.hasNext()){
Student s=it2.next();
if(s.getMath()<60 && s.getEglish()<60 && s.getHistory()<60)
System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
}
System.out.println();
System.out.println("三门至少有一门通过的同学为:");
Iteratorit3=list.iterator();
while(it3.hasNext()){
Student s=it3.next();
if(s.getMath()>=60 || s.getEglish()>60 || s.getHistory()>60)
System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
}
System.out.println();
System.out.println("数学不通过的同学为:");
Iteratorit4=list.iterator();
while(it4.hasNext()){
Student s=it4.next();
if(s.getMath()<60)
System.out.println("学号:"+s.getId()+" 姓名:"+s.getName()+" 数学:"+s.getMath()+" 英语:"+s.getEglish()+" 历史"+s.getHistory());
}
System.out.println();
System.out.println("使用for语句");
System.out.println("所有学生成绩:");
for(int i=0;i
System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
System.out.println();
System.out.println("三门全部通过的同学为:");
for(int i=0;i
if(list.get(i).getMath()>=60 && list.get(i).getEglish()>=60 && list.get(i).getHistory()>=60)
System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
}
System.out.println();
System.out.println("三门全部不通过的同学为:");
for(int i=0;i
if(list.get(i).getMath()<60 && list.get(i).getEglish()<60 && list.get(i).getHistory()<60)
System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
}
System.out.println();
System.out.println("三门至少有一门通过的同学为:");
for(int i=0;i
if(list.get(i).getMath()>60 || list.get(i).getEglish()>60 || list.get(i).getHistory()>60)
System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
}
System.out.println();
System.out.println("数学不通过的同学为:");
for(int i=0;i
if(list.get(i).getMath()<60)
System.out.println("学号"+list.get(i).getId()+" 姓名 "+list.get(i).getName()+" 数学"+list.get(i).getMath()+" 英语"+list.get(i).getEglish()+" 历史"+list.get(i).getHistory());
}
System.out.println();
}
}