课堂水王2

设计思路

 对数组进行排序,依次确定数组成员的出现次数,当不相符时,输出成员及其次数

实验代码

package yy;
import java.util.*;
public class test {
	public static void main(String[] args) {
		int array[]={5, 10, 10, 5, 2, 5, 3, 5, 10, 5, 2, 5, 5, 10, 1, 5, 1};
		Arrays.sort(array);// 给数组排序
		int count=0;
		int temp=array[0];
		for(int i=0;i<array.length;i++)
		{
		   if(temp !=array[i]){
		   System.out.println(temp+"水王:"+count+"次");
		   temp = array[i];
		   count=1;
		}
		else
		{
		 count++;
		}
		if(i==array.length-1)
		{
		    System.out.println(temp+"水王:"+count+"次");
		}
		}
		
	}

	
}

实验截图

实验总结

  输出一个成员,记得数组次数开始要重新为1;

原文地址:https://www.cnblogs.com/gdp176119/p/5533831.html