我需要开发一个java
程序,要求用户输入一些integers
并找到最大,最小的数字和这些数字的平均值。然后,将数组分成用户指定的多个子区间,然后生成一个边界点,每个边界点的子区间宽度为...
问题是我需要创建一个频率:
ex:Interval:
14.5-16.5
频率: 1(这里应该显示有多少整数属于这个边界)
间隔时间:
16.5-18.5
频率:4等等。
这是我到目前为止的代码,除了找到每个边界的频率外几乎完成了。
import java.util.Scanner;
public class Sta
{
public static void main(final String args[])
{
final Scanner input = new Scanner(System.in);
int num=0;
int range=0;
int subnum;
int subwid;
System.out.print("How many numbers do you want to enter: ");
num=input.nextInt();
final int array[]=new int[num];
System.out.print("Enter the numbers now: ");
for(int i=0; i<array.length; i++)
{
array[i]=input.nextInt();
}
System.out.print("These are the numbers you entered:\n");
printArray(array);
int smallest=array[0];
int largest=array[0];
for (final int element : array) {
if(element>largest) {
largest=element;
} else if(element<smallest) {
smallest=element;
}
range=largest-smallest;
}
System.out.printf("Largest is %d\n",largest);
System.out.printf("Smallest is %d\n",smallest);
System.out.printf("Range is %d\n",range);
System.out.print("Enter the number of subinterval: ");
subnum=input.nextInt();
subwid=range/subnum;
System.out.printf("The width of subinterval is %d\n", subwid);
/* this part should find the boundaries and find the elements that fall between
the each two boundaries */
for(double boundary=smallest-.5; boundary <=largest+.5; boundary +=subwid)
{
System.out.printf("Boundaries are %.1f\n",boundary);
for(int element=0; element<array.length; element++)
{
if(element>=boundary)
{
System.out.printf("f=%d\n",element);
}
}
}
}
public static void printArray(final int arr[])
{
for (final int element : arr) {
System.out.print(element + "\n");
}
}
}
问题是如何找到上表中的频率?
答案 0 :(得分:2)
以下是一些选项: