2017-05-08 59 views
1

我正在研究我的hw,這是關於創建學生記錄系統的,我不知道爲什麼我的代碼無法顯示我的所有輸入。你們可以給我一些解決這個問題的建議嗎? 我的預期輸出爲0:第一個名字,姓氏,ID,學號,成績如何顯示來自另一個類(java)的輸入

這裏是我的代碼:

import java.util.Scanner;` 
    public class semifinal{ 
     static String[][] records = new String[50][50]; 
     static int num = 0; 
     public static void main(String args[]){ 
      Scanner kb = new Scanner(System.in); 
      System.out.println("Welcome to the Student record management system!"); 
      System.out.println(); 
      int i, n = 0; 
      while(true){ 
       System.out.println("enter 'input' for inputting student's records"); 
       System.out.println("enter 'update' for updating the student's records"); 
       System.out.println("enter 'search' for searching the student's records"); 
       System.out.println("enter 'display' for displaying all students according to the final exam scores."); 
      String word = kb.nextLine(); 
      if(word.equals("input")){ 
       System.out.println("Input student["+num+"]:"); 
       System.out.println("input <first name> "); 
       String firstname = kb.nextLine(); 
       System.out.println("input <last name> "); 
       String lastname = kb.nextLine(); 
       System.out.println("input <ID> "); 
       String id = kb.nextLine(); 
       System.out.println("input <Student Number>"); 
       String num = kb.nextLine(); 
       insert(firstname, lastname, id, num); 
      }else if(word.equals("update")){ 
       System.out.println("Enter the student number to update the student records"); 
       String num = kb.nextLine(); 
       System.out.println("Update the final exam score:"); 
       String score = kb.nextLine(); 
       update(num, score); 
      }else if(word.equals("search")){ 
       System.out.println("Enter the student number to search the student records"); 
       String number = kb.nextLine(); 
       String[] input = search(number); 
       System.out.println(); 
       System.out.println(input[0]+" "+input[1]+" "+input[2]+" "+input[3]+" "+input[4]+" ");   

      }else if(word.equals("display")){ 
       Display(); 

      }else{ 
       System.out.println(); 
       System.out.println("Please enter a valid input again!"); 

      } 
     } 
    } 
    public static void insert(String firstname, String lastname, String id, String num){ 
     for(int i = 0; i<records.length; i++){ 
     records[i][0] = firstname; 
     records[i][1] = lastname; 
     records[i][2] = id; 
     records[i][3] = num; 
     i++; 
    } 
    } 
    public static void update(String num, String score){ 
     for(int i = 0; i<records.length; i++){ 
     records[i][4] = score; 
     String[] update = search(num); 
     update[4] = score; 
     } 
    } 
    public static String[] search(String number){ 
     for(int i = 0; i<records.length; i++){ 
      if(number.equals(records[i][3])){ 
       return records[i]; 
       } 
      } 
      return null; 
     } 

    public static void Display(){ 
       sort(); 
       for(int i = 0; i<num; i++){ 
        System.out.println(num+" "+records[i][0] 

+" "+records[i][1]+" "+records[i][2]+" "+records[i][3]+" "+records[i][4]); 
      } 
     } 

     public static void sort(){ 
      int[] sort = new int[num]; 
      for(int i = 0; i<num; i++){ 
       sort[i] = Integer.parseInt(records[i][4]); 
      } 
      for(int i = 0; i<num-1; i++){ 
       for(int j = i+1; j<num; j++){ 
        if(sort[j]<sort[i]){ 
         int temp = sort[j]; 
         String[] x = records[j]; 
         records[j] = records[i]; 
         records[i] = x; 
         sort[j] = sort[i]; 
         sort[i] = temp; 
        } 
       } 
      } 
     } 

這有什麼錯我的代碼?我不知道如何解決它。

+0

歡迎來到Stack Overflow!看起來你可能會問作業幫助。雖然我們本身沒有任何問題,但請觀察這些[應做和不應該](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845),並相應地編輯您的問題。 (即使這不是家庭作業,請考慮建議。) –

+0

您對代碼的期望是什麼? –

+0

抱歉,我忘了提及。 – llllau

回答

1

您遮蔽您num變量

你把它設置爲現場

int num = 0; 

但那麼你的代碼中你也有

String num = kb.nextLine(); 

也許這應該

更換
num = Integer.valueof(kb.nextLine()); 
1

代碼有許多邏輯錯誤。我在評論中強調了其中的一些。我已經部分解決了你可以繼續工作的問題。

public static void main(String[] args) { 
     Scanner kb = new Scanner(System.in); 
     System.out.println("Welcome to the Student record management system!"); 
     System.out.println(); 
     int i, n = 0; 
     while(true){ 
      System.out.println("enter 'input' for inputting student's records"); 
      System.out.println("enter 'update' for updating the student's records"); 
      System.out.println("enter 'search' for searching the student's records"); 
      System.out.println("enter 'display' for displaying all students according to the final exam scores."); 
      String word = kb.nextLine(); 
      if(word.equals("input")){ 
       System.out.println("Input student["+ ++num +"]:"); 
       System.out.println("input <first name> "); 
       String firstname = kb.nextLine(); 
       System.out.println("input <last name> "); 
       String lastname = kb.nextLine(); 
       System.out.println("input <ID> "); 
       String id = kb.nextLine(); 
       System.out.println("input <Student Number>"); 
       String studNum = kb.nextLine(); 
       insert(firstname, lastname, id, studNum); 
      }else if(word.equals("update")){ 
       System.out.println("Enter the student number to update the student records"); 
       String num = kb.nextLine(); 
       System.out.println("Update the final exam score:"); 
       String score = kb.nextLine(); 
       update(num, score); 
      }else if(word.equals("search")){ 
       System.out.println("Enter the student number to search the student records"); 
       String number = kb.nextLine(); 
       String[] input = search(number); 
       System.out.println(); 
       System.out.println(input[0]+" "+input[1]+" "+input[2]+" "+input[3]+" "+input[4]+" "); 

      }else if(word.equals("display")){ 
       Display(); 

      }else{ 
       System.out.println(); 
       System.out.println("Please enter a valid input again!"); 

      } 
     } 
    } 

無需插入循環。

public static void insert(String firstname, String lastname, String id, String studNum){ 
     int index = num -1; 
     records[index][0] = firstname; 
     records[index][1] = lastname; 
     records[index][2] = id; 
     records[index][3] = studNum; 
    } 

沒有必要在顯示器中打印索引4(註釋掉了,因爲沒有時間去修復,如果有的話)。

public static void Display(){ 
     //sort(); 
     for(int i = 0; i<num; i++){ 
      System.out.println(num+" "+records[i][0] 

        +" "+records[i][1]+" "+records[i][2]+" "+records[i][3]); 
     } 
    } 

現在它至少會打印記錄。

+0

非常感謝你!但我仍然無法打印我的輸入... – llllau

+0

我插入了一條記錄,然後選擇了顯示器來打印它正在打印 –

+0

也許是因爲我用錯誤的方式使用排序方法 – llllau

相關問題