Java 第一次使用数组,尝试创建频率表
我看不出我在这方面出了什么问题,我希望它显示调查结果的频率,但我得到的是零,例如,如果我输入1,1,2,2,5,我希望它输出:Java 第一次使用数组,尝试创建频率表,java,arrays,methods,constructor,frequency,Java,Arrays,Methods,Constructor,Frequency,我看不出我在这方面出了什么问题,我希望它显示调查结果的频率,但我得到的是零,例如,如果我输入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 c
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();
}
}
import java.util.Scanner;
公共类调查员{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
有多少人参加了调查;
int numPeople=input.nextInt();
调查s=新调查(numPeople);
int nextResponse;
for(int i=0;i
这是我的班级:
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]);
}
}
}
公共类调查
{
私人国际核反应;
私有int-maxResponses;
私有int[]响应;
专用int[]频率;
私人最终int NUM_响应=5;
公众调查(int)
{
maxResponses=nResponses;
numResponses=0;
响应=新的int[n响应];
frequencies=new int[NUM_RESPONSES];//调查有5种可能的回答
}
公共void addResponse(int响应)
{
//此方法存储响应
//传入参数“response”
//到阵列中的下一个可用空间
{
响应[numResponses]=响应;
}
numResponses++;
}
public void displayAllResponses()
{
//此方法显示在屏幕上
//所有对调查的回应
对于(int i=0;i您没有使用频率
数组执行任何操作。在addResponse
方法中,您需要获取适当的值并将其递增。添加如下语句
频率[response-1]=频率[response-1]+1;
请注意,您可以使用2个数组来实现这一点,尤其是在学习时,但是很多Java开发人员会使用Map
实现来实现这一点,其中键是输入的值,值是频率。将所有数据保留在一个数据结构中。公共类重复数组{
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." );
}
}
}
}
公共静态void main(字符串[]args){
整数计数=0;
int[]arr=新的int[6];
int[]frequencyArr=新的int[6];
arr[0]=4;
arr[1]=1;
arr[2]=1;
arr[3]=1;
arr[4]=5;
arr[5]=4;
对于(int i=0;i
整数数组中的默认值为零。每次出现时,我们都会将其递增一。
示例:如果整数4出现两次,则频率arr处的第四个索引将增加两次。频率[3]++;
可以是频率[responses[i]++;
,从而消除If
语句(另请参见:开关
语句)。您是如何编写和编译代码的?如果您使用的是NetBeans或Eclipse之类的IDE,我强烈建议您学习如何使用内置调试器。使用它,您可以一次一行地遍历代码并查看变量值。或者,您可以添加System.out.println()
调用您的代码来打印跟踪信息和变量值。调试对于任何程序员来说都是一项关键技能。我正在使用记事本++和记事本++中的内部编译器。@code Guru我会接受您的建议,并因此切换到Eclipse,谢谢。@Kingboom4记事本++是一个很棒的编辑器,因为它非常简单。如果y如果您想坚持下去,可以使用我稍后的建议,在整个代码中添加System.out.println()
调用。有些可以是简单的消息,例如“我在这里”只是告诉您执行了哪行代码。其他代码可以打印出变量的值。有时,快速SOP比启动调试器简单得多,因此知道如何执行这两项操作将使您成为一名更好的程序员。