2017-05-09 178 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

相关问题