2017-05-08 5 views
1

私は学生レコードシステムを作成することについての私の仕事に取り組んでいます。なぜ私のコードがすべての入力を表示できないのかわかりません。あなたは私にこの問題を解決するための提案をしてもらえますか?私の予想出力は です: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

ようこそスタックオーバーフロー!宿題の助けを求めているようです。それ自体に問題はありませんが、これらのことを守ってください(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!"); 

      } 
     } 
    } 

ループインサートの必要はありません。

インデックス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

レコードを1つ挿入してから印刷するように選択しましたが、印刷されています。 –

+0

多分、私は間違った方法でソートメソッドを使用しています。 – llllau

関連する問題