我需要開發一個java
程序,要求用戶輸入一些integers
並查找這些數字的最大值,最小值和平均值。然後,將所述組陣列的成多個子間隔用戶指定,則它產生一個邊界點分別具有子區間寬度的長度..java:在一個數組中查找整數的頻率
的問題是,我需要創建頻率:
例如:間隔:
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");
}
}
}
的問題是我怎麼找到頻率,在上表例子?
無縮進的代碼是不可讀的。我爲你縮進了。 –
那麼你有什麼困惑? –
謝謝肖恩.. –