2017-05-09 148 views
-1
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) 
在主代碼

以上

+3

S第1步:正確縮進你的代碼。第2步:將你的代碼分解成方法。 – domsson

+2

您的代碼對我來說不可讀,我懷疑您能夠閱讀它。請使用正確的縮進並提取一些具有正確名稱的方法。 –

+0

發佈顯示條件和輸入的[mcve]。條件內的代碼是無用的,除非他們更新輸入值。其餘的,請閱讀上面的註釋 – AxelH

回答

0

我認爲問題出在線路的pos值

stud.add(pos, new Student(name, matric, cgpa)); 

你確定pos是一個有效的索引?從add documentation

拋出:
IndexOutOfBoundsException - 如果索引超出範圍(index < 0 ||指數>尺寸())

EDIT

爲了處理這種是你可以做的問題

if (stud.size() < pos) 
    //error (print error message?) 
else { 
    // go on with other instructions 
} 
+0

我不確定。實際上它並不完整我的代碼。這是小組任務。以及我如何解決這個問題? –

+2

特別是如果這是一個團隊的努力,你*真的*,**真的**應該將該代碼拆分成一些明確定義的,正確命名的方法。如果我是你,那就是我首先要做的。馬上。 – domsson

相關問題