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