鱼C论坛

 找回密码
 立即注册
查看: 3554|回复: 10

[已解决]编写应用程序

[复制链接]
发表于 2020-4-23 11:11:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
编写应用程序,程序运行后生成100组36选7的数组,并统计1~36中的各个数字出现的次数。
最佳答案
2020-4-23 13:13:05
  1. import java.util.Random;


  2. public class RandomCount {
  3.     public static void main(String[] args) {
  4.         int[][] numTable=generateNum();//生成数组
  5.         MyCount(numTable);//统计并打印数组中每个数字的出现次数

  6.     }

  7.     /**
  8.      *
  9.      * @param arr   传入需要进行统计的数组
  10.      */
  11.     private static void MyCount(int[][] arr) {
  12.         //定义长度为36的数组来统计,具体办法是每出现一次1,a[0]计数一次;每出现一次2,a[1]计数一次......
  13.         int []count=new int[36];

  14.         for (int[] ints : arr) {
  15.             for (int anInt : ints) {
  16.                 count[anInt-1]++;
  17.             }
  18.         }

  19.         //遍历输出
  20.         for (int i = 0; i <36 ; i++) {
  21.             System.out.println((i+1)+"出现的次数为"+count[i]);
  22.         }
  23.     }

  24.     /**
  25.      *
  26.      * @return 由100x7个在1到36之间的随机数组成的二维数组
  27.      */
  28.     private static int[][] generateNum() {
  29.         Random random=new Random();
  30.         int[][] arr=new int[100][7];
  31.         for (int i = 0; i <100 ; i++) {
  32.             for (int j = 0; j <7 ; j++) {
  33.                 arr[i][j]=random.nextInt(36)+1;
  34.             }
  35.         }
  36.         return arr;
  37.     }
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-23 11:13:12 | 显示全部楼层
???
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-23 11:56:14 From FishC Mobile | 显示全部楼层
random随机数会用吗?利用它产生0到36之间的随机数,将7个随机数放入一个数组中。
再把以上代码放入一个循环中,重复100遍。
可以用100个一维数组,但更好是用一个a[100][7]的二维数组实现要求。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:13:05 | 显示全部楼层    本楼为最佳答案   
  1. import java.util.Random;


  2. public class RandomCount {
  3.     public static void main(String[] args) {
  4.         int[][] numTable=generateNum();//生成数组
  5.         MyCount(numTable);//统计并打印数组中每个数字的出现次数

  6.     }

  7.     /**
  8.      *
  9.      * @param arr   传入需要进行统计的数组
  10.      */
  11.     private static void MyCount(int[][] arr) {
  12.         //定义长度为36的数组来统计,具体办法是每出现一次1,a[0]计数一次;每出现一次2,a[1]计数一次......
  13.         int []count=new int[36];

  14.         for (int[] ints : arr) {
  15.             for (int anInt : ints) {
  16.                 count[anInt-1]++;
  17.             }
  18.         }

  19.         //遍历输出
  20.         for (int i = 0; i <36 ; i++) {
  21.             System.out.println((i+1)+"出现的次数为"+count[i]);
  22.         }
  23.     }

  24.     /**
  25.      *
  26.      * @return 由100x7个在1到36之间的随机数组成的二维数组
  27.      */
  28.     private static int[][] generateNum() {
  29.         Random random=new Random();
  30.         int[][] arr=new int[100][7];
  31.         for (int i = 0; i <100 ; i++) {
  32.             for (int j = 0; j <7 ; j++) {
  33.                 arr[i][j]=random.nextInt(36)+1;
  34.             }
  35.         }
  36.         return arr;
  37.     }
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:14:31 | 显示全部楼层
饭后休息,随手敲了个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:21:48 | 显示全部楼层
倒戈卸甲 发表于 2020-4-23 13:14
饭后休息,随手敲了个

感谢  这个代码 确实是不会   按照你的敲了 一遍   执行后   比如输入  I love fish   只显示 一个 I   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:24:52 | 显示全部楼层
aurora54204781 发表于 2020-4-23 13:21
感谢  这个代码 确实是不会   按照你的敲了 一遍   执行后   比如输入  I love fish   只显示 一个 I

不是 这个 是 刚才那个     
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 13:25:39 | 显示全部楼层
  1. package per.whj.zwks.ch01;

  2. import java.util.*;
  3. public class Text6 {
  4. public static void main(String[] args) {
  5. Random r = new Random();
  6. List list = new ArrayList<HashSet>();
  7. for (int i = 0; i < 100; i++) {
  8. Set set = new HashSet<Integer>();
  9. int j = 0;
  10. while (j < 7) {
  11. int random = r.nextInt(37);
  12. if (!set.contains(random) && random > 0) {
  13. set.add(random);
  14. j++;
  15. }
  16. }
  17. list.add(set);
  18. }
  19. int a[] = new int[700];
  20. int j = 0;
  21. for (int i = 0; i < list.size(); i++) {
  22. HashSet set = (HashSet) list.get(i);
  23. Iterator it = set.iterator();
  24. while (it.hasNext()) {
  25. a[j] = Integer.parseInt(it.next().toString());
  26. j++;
  27. }
  28. }
  29. Arrays.sort(a);
  30. int count = 0;
  31. int c, d;
  32. c = d = 1;
  33. for (int i = 0; i < a.length; i++) {
  34. c = a[i];
  35. if (d != c) {
  36. System.out.println("出现" + d + ",次数:" + count);
  37. d = c;
  38. count = 0;
  39. }
  40. count++;
  41. }
  42. }
  43. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 13:28:45 From FishC Mobile | 显示全部楼层
aurora54204781 发表于 2020-4-23 13:21
感谢  这个代码 确实是不会   按照你的敲了 一遍   执行后   比如输入  I love fish   只显示 一个 I

我肯定是运行过才发给你的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 14:26:19 | 显示全部楼层
倒戈卸甲 发表于 2020-4-23 13:28
我肯定是运行过才发给你的

好 我再试试  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 14:30:11 From FishC Mobile | 显示全部楼层
本帖最后由 倒戈卸甲 于 2020-4-23 14:36 编辑
aurora54204781 发表于 2020-4-23 13:25


这题二维数组存储数字,再用一维数组来统计非常简单。用set集合相对麻烦。而且即便为了结构清楚,使用set集合去存储100个数组,也还是用一个一维数组去统计比较方便
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 17:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表