1
所以我正在處理java中的數組,並且遇到了一個問題,我試圖列出f中長度爲100的數組中最少出現數的位置,範圍爲0-9。我正在使用第二個數組來計算出現次數,直到我的最後一段代碼才完成。在java中使用數組
import java.util.Random;
public class Program6
{
public static void main(String[] args)
{
int[] bigArray = new int[100];
int[] count = new int[10];
for (int i = 0; i < bigArray.length ;i++)
{
bigArray[i] = (int)(Math.random() * 10);
}
for (int i = 0; i < bigArray.length ;i++)
{
System.out.println(bigArray[i] + " ");
}
for(int i = 0; i < bigArray.length; i++)
{
count[bigArray[i]]++;
}
for (int i = 0, j = 0; i < count.length ;i++, j++)
System.out.println("count for " + j +" is: " + count[i] + " ");
int min = count[0];
int indexOfMin = 0;
for (int i =1; i < count.length; i++)
{ if (count[i] < min)
{
min = count[i];
indexOfMin = i;
}
}
System.out.println("\nThe number " + indexOfMin + " only appears " + min + " times in the array.");
System.out.println("It appears in the large array at: ");
\\this guy right here
for (int i = 0 ; i < bigArray.length; i++)
{ if (bigArray[i] == min)
{
System.out.println("bigArray["+ i + "]");
}
}
}
}
應該只列出關閉陣列的位置。但它返回的方式不僅僅是最低出現整數的位置,即使限定符使用變量分鐘。
什麼給?我錯過了什麼?
沒有輸入,即返回輸出是
8
..
4
count for 0 is: 18
count for 1 is: 6
count for 2 is: 9
count for 3 is: 9
count for 4 is: 16
count for 5 is: 4
count for 6 is: 12
count for 7 is: 8
count for 8 is: 7
count for 9 is: 11
The number 5 only appears 4 times in the array.
It appears in the large array at:
bigArray[3]
bigArray[6]
bigArray[10]
bigArray[23]
bigArray[25]
bigArray[39]
bigArray[44]
bigArray[48]
bigArray[49]
bigArray[50]
bigArray[51]
bigArray[57]
bigArray[58]
bigArray[67]
bigArray[84]
bigArray[99]
這是完美的,除了bigArray []列表輸出。它應該只顯示索引何時數組等於最少出現的數字。
請出示的輸入,實際輸出與預期輸出。 –
我發現了一個我不喜歡的縮進風格,而不僅僅是新的大括號! –
如果最小值是5,並且兩個(或更多)數字出現5次,該怎麼辦? –