我需要输出此代码的输出按照输入控制台的顺序。目前,输出大多数是出现的顺序。不确定如何更改它以返回结果按顺序输入。例如,我需要: 输入七个数字:12 23 44 22 23 22 55 回来 12号出现1次 23号出现2次 44号出现1次 22号出现2次 55号出现1次
import java.util.*;
import java.util.Map.Entry;
public class CountOccurrences7Integers {
public static void main(String[] args) {
System.out.println("Alina's Copy ");
System.out.print("Enter Seven Numbers :");
// An array to hold seven elements
int[] numbers = new int[7];
// loop reads input up to 7 integers from the console
Scanner input = new Scanner(System.in);
for (int i = 0; i < 7; i++) {
if (input.hasNextInt()) {
numbers[i] = input.nextInt();
}
}
//countMap holds the count details of each element
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++)
{
int key = numbers[i];
if (countMap.containsKey(key))
{
int count = countMap.get(key);
count++;
countMap.put(key, count);
} else
{
countMap.put(key, 1);
}
}
//Printing the Element and its occurrence in the array
for(Entry<Integer, Integer> val : countMap.entrySet())
{
System.out.println("Number "+val.getKey() + " occurs " + val.getValue() + " times");
}
input.close();
}
}