在此代码中,我想比较所有4名测试学生并显示谁失败。我只需要Failed Class中的帮助。顶级课很好。
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentClass {
static ArrayList<Student> list = new ArrayList<Student>();
static void Student() {
Student topper = list.get(0);
for(int i=1;i<list.size();++i){
if(topper.marks<list.get(i).marks){
topper = list.get(i);
}
}
System.out.println("tooper is : "+topper.name);
}
static void failed1(){
if(list.get(i)<=35 ){
}
}
// System.out.println("This Student Failed" +);
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s = new Student();
s.name = "Test 1";
s.roolno = 1;
s.marks = 30.5f;
list.add(s);
Student x = new Student();
x.name = "Test 2";
x.roolno = 2;
x.marks = 32.5f;
list.add(x);
Student y = new Student();
y.name = "Test 3";
y.roolno = 3;
y.marks = 80.5f;
list.add(y);
Student z = new Student();
z.name = "Test 4";
z.roolno = 4;
z.marks = 90.5f;
list.add(z);
printMainMenu();
}
private static void printMainMenu() {
System.out.println("** Main Menu **");
System.out.println("1.Topper");
System.out.println("2. Student Who Failed");
System.out.println("3.Exit");
System.out.println("\nEnter your Choice");
Scanner in = new Scanner(System.in);
int ch = in.nextInt();
switch (ch) {
case 1:
Student();
break;
case 2:
failed1();
break;
case 3:
System.exit(0);
default:
System.out.println("Enter Vaild Press Enter To Continue");
try {
System.in.read();
clear();
printMainMenu();
} catch (IOException e) {
e.setStackTrace(null);
}
break;
}
}
private static void clear() {
// TODO Auto-generated method stub
}
private static void failed() {
// TODO Auto-generated method stub
}
}
// private static void Entering(String string) {
// TODO Auto-generated method stub
答案 0 :(得分:0)
您可以通过几种方式进行检查。最简单的当然是手动检查
ArrayList<Student> failedStudents = new ArrayList<Student>();
for (Student student : studentList)
{
if (student.getMark() < passMark )
{
failedStudents.add(student);
}
}
您还可以使用PriorityQueue
类来实现Comparator
,以按标记排序Student
个对象。