import java.util.LinkedList;
import java.util.*;
public class studLink {
public static void main(String[] args) {
LinkedList<Student> stud = new LinkedList<>();
Scanner scan = new Scanner(System.in);
System.out.println("Welcome To Student Data Manager");
while (true) {
try {
int a;
do {
System.out.println("1. Add first\n2. Add between\n3. Add last");
a = scan.nextInt();
if (a == 1) {
System.out.print("Student name : ");
String name = scan.next();
System.out.print("Student matric : ");
String matric = scan.next();
System.out.print("Student CGPA : ");
double cgpa = scan.nextDouble();
stud.addFirst(new Student(name, matric, cgpa));
try {
int c;
do {
System.out.println("1. Add more\n2. Delete one\n3. Print dean list");
c = scan.nextInt();
if (c == 1) {
System.out.println("Add More");
} else if (c == 2) {
try {
int e;
do {
System.out.println("1. Delete first\n2. Delete Between\n3. Delete last");
e = scan.nextInt();
if (e == 1) {
stud.removeFirst();
} else if (e == 2) {
System.out.print("Position to remove : ");
int f = scan.nextInt();
stud.remove(f);
} else if (e == 3) {
stud.removeLast();
} else {
System.out.println("Please enter available number");
}
} while (e != 1 && e != 2 && e != 3);
} catch (Exception e) {
}
} else if (c == 3) {
System.out.println("Dean List");
for (Student st : stud) {
int i = 1;
i++;
if (st.getCgpa() >= 3.75) {
System.out.println("-------Student " + i + " -------");
System.out.println("Name = " + st.getName());
System.out.println("Matric No = " + st.getMatric());
System.out.println("CGPA = " + st.getCgpa());
}
break;
}
} else {
System.out.println("Please enter available number");
}
} while (c != 1 && c != 2 && c != 3);
} catch (Exception e) {
}
}
else if (a == 2) {
System.out.print("Enter position : ");
int pos = scan.nextInt();
System.out.print("Student name : ");
String name = scan.next();
System.out.print("Student matric : ");
String matric = scan.next();
System.out.print("Student CGPA : ");
double cgpa = scan.nextDouble();
stud.add(pos, new Student(name, matric, cgpa));
try {
int c;
do {
System.out.println("1. Add more\n2. Delete one\n3. Print dean list");
c = scan.nextInt();
if (c == 1) {
System.out.println("Add More");
} else if (c == 2) {
try {
int e;
do {
System.out.println("1. Delete first\n2. Delete Between\n3. Delete last");
e = scan.nextInt();
if (e == 1) {
stud.removeFirst();
} else if (e == 2) {
System.out.print("Position to remove : ");
int f = scan.nextInt();
stud.remove(f);
} else if (e == 3) {
stud.removeLast();
} else {
System.out.println("Please enter available number");
}
} while (e != 1 && e != 2 && e != 3);
} catch (Exception e) {
}
} else if (c == 3) {
System.out.println("Dean List");
for (Student st : stud) {
int i = 1;
i++;
if (st.getCgpa() >= 3.75) {
System.out.println("-------Student " + i + " -------");
System.out.println("Name = " + st.getName());
System.out.println("Matric No = " + st.getMatric());
System.out.println("CGPA = " + st.getCgpa());
}
break;
}
} else {
System.out.println("Please enter available number");
}
} while (c != 1 && c != 2 && c != 3);
} catch (Exception e) {
}
}
else if (a == 3) {
System.out.print("Student name : ");
String name = scan.next();
System.out.print("Student matric : ");
String matric = scan.next();
System.out.print("Student CGPA : ");
double cgpa = scan.nextDouble();
stud.addLast(new Student(name, matric, cgpa));
try {
int c;
do {
System.out.println("1. Add more\n2. Delete one\n3. Print dean list");
c = scan.nextInt();
if (c == 1) {
System.out.println("Add More");
} else if (c == 2) {
try {
int e;
do {
System.out.println("1. Delete first\n2. Delete Between\n3. Delete last");
e = scan.nextInt();
if (e == 1) {
stud.removeFirst();
} else if (e == 2) {
System.out.print("Position to remove : ");
int f = scan.nextInt();
stud.remove(f);
} else if (e == 3) {
stud.removeLast();
} else {
System.out.println("Please enter available number");
}
} while (e != 1 && e != 2 && e != 3);
} catch (Exception e) {
}
} else if (c == 3) {
System.out.println("Dean List");
for (Student st : stud) {
int i = 1;
i++;
if (st.getCgpa() >= 3.75) {
System.out.println("-------Student " + i + " -------");
System.out.println("Name = " + st.getName());
System.out.println("Matric No = " + st.getMatric());
System.out.println("CGPA = " + st.getCgpa());
}
break;
}
} else {
System.out.println("Please enter available number");
}
} while (c != 1 && c != 2 && c != 3);
} catch (Exception e) {
}
} else {
System.out.println("Please enter available number");
}
} while (a != 1 && a != 2 && a != 3);
} catch (Exception e) {
}
}
}
}
问题是如果我选择了1号和3号,下一个代码就可以了。但如果我输入2,它会跳过剩下的代码。我使这段代码循环回到第一个进度完成。
这是示例问题输出
Welcome To Student Data Manager
1. Add first
2. Add between
3. Add last
2 //this is input number
Enter position : 2
Student name : wqefq
Student matric : 434
Student CGPA : 2
1. Add first
2. Add between
3. Add last
当我输入2 时,我需要的是这样的
Welcome To Student Data Manager
1. Add first
2. Add between
3. Add last
1 //this is input number
Student name : hoiaufh
Student matric : 2344
Student CGPA : 4
1. Add more
2. Delete one
3. Print dean list
主要选项是
if(a==1)
else if(a==2)
else if(a==3)
在上面的主要代码中
答案 0 :(得分:0)
我认为问题在于行的pos值
stud.add(pos, new Student(name, matric, cgpa));
你确定pos是一个有效的索引吗?来自add documentation:
抛出:
IndexOutOfBoundsException - 如果索引超出范围(索引&lt; 0 || index&gt; size())
编辑
要解决这个问题,你可以做到
if (stud.size() < pos)
//error (print error message?)
else {
// go on with other instructions
}