import java.util.*;
public class Test {
public static void main(String[] args)
{
int[] number = new int[50];
int index = 0;
boolean swap = true;
int temp;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Number: ");
System.out.println(" ");
do
{
int input = keyboard.nextInt();
if (input != -999)
number[index++] = input;
else
break;
} while (index != 0);
int[] newNumbers = new int[index];
for (int i = 0; i < index; i++)
newNumbers[i] = number[i];
System.out.println("\nNumbers\t" + "Occurances");
goBack: for (int i = index - 1; i >= 0; i--)
{
for (int n = index - 1; n > i; n--)
if (newNumbers[n] == newNumbers[i])
continue goBack;
int count = 0;
for (int n = 0; n < index; n++)
if (newNumbers[n] == newNumbers[i])
count++;
for(int s=0; s < newNumbers.length-1; s++){
for(int j=1; j < newNumbers.length-s; j++){
if(newNumbers[j-1] > newNumbers[j]){
temp=newNumbers[j-1];
newNumbers[j-1] = newNumbers[j];
newNumbers[j] = temp;
}
}
}
System.out.println( newNumbers[i] + " " + count);
}
}
}
该代码旨在通过键盘扫描仪获取输入。将输入的整数进行比较,并对数组[]的不同元素列表进行排序和打印。但是,输入列表包含一些元素的倍数。重复的元素在计数中标记。最终输出应该是不同数组元素的列表(没有重复),从最大到最小的顺序与它们各自的计数。
输入如下:-12,3,-12,4,1,1,-12,1,-1,1,2,3,4,2,3,-12 当排序通过并打印时,索引4在计数为2时计数为4.我尝试了选择,冒泡和交换排序算法都具有相似的结果。任何建议将不胜感激:)。
答案 0 :(得分:1)
希望这会有所帮助,但可能是比这更好的解决方案:
public static void main(String[] args) {
int[] numbers = new int[50] ;
int index = 0;
int temp;
Scanner keyboard = new Scanner(System.in);
// get the user input
System.out.print("Enter Number: ");
System.out.println(" ");
do {
int input = keyboard.nextInt();
if (input != -999)
numbers[index++] = input;
else
break;
} while (index != 0);
keyboard.close();
System.out.println("\nNumbers\t" + "Occurances");
// create a new array and store the user input
int[] newNumbers = new int[index];
for (int i = 0; i < index; i++)
newNumbers[i] = numbers[i];
// sort the array
for (int s = 0; s < newNumbers.length - 1; s++) {
for (int j = 1; j < newNumbers.length - s; j++) {
if (newNumbers[j - 1] < newNumbers[j]) {
temp = newNumbers[j - 1];
newNumbers[j - 1] = newNumbers[j];
newNumbers[j] = temp;
}
}
}
System.out.println(Arrays.toString(newNumbers));
int count = 1;
int prevElement = 0;
if (newNumbers.length > 0) {
prevElement = newNumbers[0];
}
// print the results
for (int x = 1; x < newNumbers.length; x++) {
if (newNumbers[x] == prevElement) {
count++;
} else {
System.out.println(prevElement + " occurs " + count + "times");
prevElement = newNumbers[x];
count = 1;
}
}
System.out.println(prevElement + " occurs " + count + "times");
}
答案 1 :(得分:0)
嗨如果你真的想要更短的一个::
import java.util.*;
public class Test {
public static void main(String[] args)
{
Integer number[] = new Integer[50];
int index = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Number: ");
System.out.println(" ");
do
{
int input = keyboard.nextInt();
if (input != -999)
number[index++] = input;
else
break;
} while (index != 0);
List<Integer> asList = Arrays.asList(number);
for(Integer n: asList){
if(n != null)
System.out.println(n + " occurance " + Collections.frequency(asList,n));
}
}
}