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)
在主代码
以上
S第1步:正确缩进你的代码。第2步:将你的代码分解成方法。 – domsson
您的代码对我来说不可读,我怀疑您能够阅读它。请使用正确的缩进并提取一些具有正确名称的方法。 –
发布显示条件和输入的[mcve]。条件内的代码是无用的,除非他们更新输入值。其余的,请阅读上面的注释 – AxelH