1

これでどこが間違っているのかわかりません。調査結果の頻度を表示したいのですが、たとえば1,1,2,2,5と入力すると、ゼロだけが表示されます。出力する:

Displaying response frequencies...
1   2
2   2
3   0
4   0
5   1

しかし、代わりに私はこれを取得します:

Displaying response frequencies...
1   0
2   0
3   0
4   0
5   0

これが私の主な方法です:

import java.util.Scanner;
public class SurveyTester {

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

        System.out.println("How many people took the survey?");
        int numPeople = input.nextInt();
        Survey s = new Survey(numPeople);
        int nextResponse;
        for (int i = 0; i < numPeople; i++)
        {
            System.out.print("Input the next survey response (values 1-5):");

            nextResponse = input.nextInt();
            s.addResponse(nextResponse);
        }

        System.out.println("Displaying all responses...");
        s.displayAllResponses();
        System.out.println("Displaying response frequencies...");
        s.displayResponseFrequencies();
     }

}

これが私のクラスです:

public class Survey
{
    private int numResponses;
    private int maxResponses;
    private int[] responses;
    private int[] frequencies;
    private final int NUM_RESPONSES = 5;

    public Survey(int nResponses)
    {
        maxResponses = nResponses;
        numResponses = 0;
        responses = new int[nResponses];
        frequencies = new int[NUM_RESPONSES]; // There are 5 possible responses to the survey
    }

    public void addResponse(int response)
    {
        // This method stores the response
        // passed in the parameter 'response'
        // to the next free space in the array

        {
            responses[numResponses]= response;
        }

        numResponses++;
    }


    public void displayAllResponses()
    {
        // This method displays on the screen
        // all the responses to the survey

        for (int i=0; i<maxResponses; i++)
        {
        System.out.print(responses[i] + " ");
        }

        System.out.println("");
    }


    public void calculateResponseFrequencies()
    {
        // This method calculates the number of
        // responses for each possible answer
        // to the survey, 1, 2, 3, 4 or 5
        // It stores the frequencies in the
        // array 'frequencies'

        for (int i=1; i<=maxResponses; i++)
        {
            if (responses[i] == 1)
            {
                frequencies[1]++;
            }

            else if (responses[i] == 2)
            {
                frequencies[2]++;
            }

            else if (responses[i] == 3)
            {
                frequencies[3]++;
            }

            else if (responses[i] == 4)
            {
                frequencies[4]++;
            }

            else if (responses[i] == 5)
            {
                frequencies[5]++;
            }
        }
    }

    public void displayResponseFrequencies()
    {
        // This method displays the response
        // frequences for each of the possible
        // response, 1, 2, 3, 4 or 5

        for (int i=0; i<frequencies.length; i++)
        {
        System.out.println((i+1) + "\t" + frequencies[i]);
        }
    }

}
4

2 に答える 2

3

frequenciesアレイでは何もしていません。addResponseメソッドでは、適切な値を取得してインクリメントする必要があります。次のようなステートメントを追加します

frequencies[response-1] = frequencies[response-1] + 1;

特に学習のために、2つの配列でこれを行うことができますが、多くのJava開発者はMap、キーが入力された値であり、値が頻度であるこの種の実装を使用することに注意してください。すべてのデータを1つのデータ構造に保持します。

于 2013-01-08T22:11:01.413 に答える
0
public class DuplicatesInArray {

  public static void main(String[] args) {
    int count = 0;
    int[] arr = new int[6];
    int[] frequencyArr = new int[6];
       arr[0] = 4;
       arr[1] = 1;
       arr[2] = 1;
       arr[3] = 1;
       arr[4] = 5;
       arr[5] = 4;

      for(int i = 0;i < arr.length;i++){

       frequencyArr[arr[i]] =  frequencyArr[arr[i]] + 1;

      }
       for(int x = 0;x<frequencyArr.length;x++){
            if(frequencyArr[x] > 0){
               System.out.println(x + " " + frequencyArr[x] + " times." );

             }
       }
    }
}

整数配列のデフォルト値はゼロになります。発生するたびに1つずつインクリメントします。例:整数4が2回発生する場合、frequencyArrの4番目のインデックスは2回インクリメントされます。

于 2014-11-14T11:06:36.043 に答える