0

問題:

ドロップ期間の終了後に学生の成績のリストを更新するプログラム。プログラムは、ドロップした学生のユーザー番号とそのインデックスから読み取り、残りの学生の成績を新しい配列にコピーする必要があります。さらに、プログラムは元のリストと更新されたリストの両方を表示する必要があります。[ヒント: 新しい配列には、残りの生徒数に相当する適切な長さが必要です]

私のコード:

public class Q5{
  static Scanner scan = new Scanner (System.in);
  public static void main (String args[]){

    double [] list={1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10};
    System.out.println("enter number of students who dropped:");
    int num=scan.nextInt();
    double [] list2 = new double [num];

    System.out.println("Enter index Of  the student who dropped ");
    for (int j=1 ; j<=num ; j++)
    {
      System.out.println("student" + j + ":");
      int index=scan.nextInt();
      list[index]=0;
    }

    int  j=0;
    for(int i=0; i<list.length ; i++)
    if (list[i]!=0)
    {
      list2[j]=list[i];
      j++;
    }
    System.out.print("The original list : " );
    for(int i=0; i<list.length ; i++)
    System.out.print(list[i] + " " );

    System.out.print("remaining students " );
    for(int i=0; i<list2.length ; i++)
    System.out.print(list2[i] + " " );
  }
}

何が問題ですか ?それは機能していません!!! 16行目には次のように書かれています:

スレッド「メイン」での例外 java.lang.ArrayIndexOutOfBoundsException: 4 at Q5.main(Q5.java:23)

これを修正するにはどうすればよいですか

4

7 に答える 7

2

それはあなたのサイズのためですlist2

そのサイズをnumに設定するのではなく、元のリストのサイズに設定する必要があります。list2には元の と同じ数の要素が含まれるためlistです。ユーザーからそれだけ多くの入力をフェッチし、それらのインデックスに値0numを割り当てるだけで済みます。

double[] list2 = new double[list.length]; // This should be the size of your list2

元のサイズを維持する必要がない場合は、@Joetjahで提案されているように、元のサイズから num を減算する必要があります。

double[] list2 = new double[list.length - num]; // This should be the size of your list2
于 2013-11-13T09:42:45.397 に答える
0

ユーザーが入力した値が配列の範囲外の場合、コードは失敗します。

int index=scan.nextInt();
list[index]=0; // This may fail. Value entered by the user may exceed the array length
于 2013-11-13T09:43:39.420 に答える
0

擬似コード:

10 人の生徒から始めます。2 人の生徒をドロップするとします。つまり、list2サイズを 2 に設定します。

list次に行うことは、 からまでのすべての生徒 (10 人) を追加しようとすることですlist2。しかし、これにlist2は小さすぎます。したがって、例外が発生します。

あなたが望むのは、これです:

double [] list2 = new double [list.length - num];
于 2013-11-13T09:43:43.977 に答える
0

事前定義された配列「リスト」の問題を推測してください。

ステップ 1: 初期化方法の 1 つを使用してリストを作成しました。ここでサイズが決まりました。

ステップ 2: ユーザーから入力を取得: 11 であると仮定します。

listは、入力として指定されたサイズを含む必要はありません。

于 2013-11-13T09:44:24.297 に答える
0

list2間違ったサイズで初期化します。

ドロップされた学生のユーザー数から取得し、残りlist2のすべての学生を入れようとします。

double [] list2 = new double [list.length - num];
于 2013-11-13T09:47:11.613 に答える
0

正常に動作しています

java.util.Scanner をインポートします。

public class Q5 {
    static Scanner scan = new Scanner(System.in);

    public static void main(String args[]) {

        double[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        System.out.println("enter number of students who dropped:");
        int num = scan.nextInt();
        double[] list2 = new double[num - 1]; // should be num-1

        System.out.println("Enter index Of  the student who dropped ");
        for (int j = 0; j < num; j++) {
            System.out.println("student" + j + ":");
            int index = scan.nextInt();
            list[index] = 0;
        }

        int j = 0;
        for (int i = 0; i < num; i++) {
            if (list[i] != 0) {
                list2[j] = list[i];
                j++;
            }
        }
        System.out.print("The original list : ");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }

        System.out.print("remaining students ");
        for (int i = 0; i < list2.length; i++) {
            System.out.print(list2[i] + " ");
        }
    }
}

出力は

 enter number of students who dropped:
    2
    Enter index Of  the student who dropped 
    student0:
    1
    student1:
     2
    The original list : 1.0 0.0 0.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 remaining students 1.0 
于 2013-11-13T09:49:42.313 に答える
-1

これにより、取得した例外が削除されます。次回からは静的な値を使用しません。

編集: 自分で変数に値を割り当てることはお勧めできません。配列のサイズは 10 であるため、ユーザーが 10 人を超える学生を入力すると、問題が発生します。

import java.util.*;

public class Q5 {
    static Scanner scan = new Scanner(System.in);

    public static void main(String args[]) {

        double[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        System.out.println("enter number of students who dropped:");
        int num = scan.nextInt();
        double[] list2 = new double[list.length-num]; // should be num-1

        System.out.println("Enter index Of  the student who dropped ");
        for (int j = 0; j < num; j++) {
            System.out.println("student" + j + ":");
            int index = scan.nextInt();
            list[index] = 0;
        }

        int j = 0;
        for (int i = 0; i < list.length; i++) {
            System.err.println(""+list[i]);
            if (list[i] > 0) {

                list2[j] = list[i];
                j++;
            }
        }
        System.out.print("The original list : ");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }

        System.out.print("remaining students ");
        for (int i = 0; i < list2.length; i++) {
            System.out.print(list2[i] + " ");
        }
    }
}
于 2013-11-13T10:36:41.270 に答える