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此类实现,其中键是输入的值,值是频率。将所有数据保存在一个数据结构中。

于 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." );

             }
       }
    }
}

整数数组中的默认值将为零。每次出现我们都会将其加一。示例:如果整数 4 出现 2 次,则频率 Arr 处的第 4 个索引增加两次。

于 2014-11-14T11:06:36.043 回答