qq_24644221 2018-07-07 08:12 采纳率: 100%
浏览 1476
已采纳

数组取值哪位大神可以帮我实现一下啊

String[] indexValue = {"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42"};

随机取数组里面的值,但是个数可以是1到42位。
比如说第一次取 出来的值是1,2,3
第二次就可以是,123456789101112.。。

  • 写回答

13条回答 默认 最新

  • LuoBinary 2018-07-07 09:08
    关注

    你定义的数组有问题,我帮你改过来了,你试下我写的

     public static void main(String[] args)
        {
            int[] indexValue = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42};
            int count = (int)(Math.random() * 42);
            System.out.println("取出数字个数:" + count);
            int[] resultIndex = new int[count];
            for (int i = 0; i < count; i++)
            {
                int index = (int)(Math.random() * 42);
                if (!containValue(resultIndex, index))
                {
                    resultIndex[i] = index;
                }
                else
                {
                    i--;
                }
            }
            for (int i = 0; i < resultIndex.length; i++)
            {
                System.out.print(indexValue[resultIndex[i]]);
                System.out.print(" ");
            }
        }
    
        private static boolean containValue(int[] resultIndex, int index)
        {
            for (int j = 0; j < resultIndex.length; j++)
            {
                if (index == resultIndex[j])
                {
                    return true;
                }
            }
            return false;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能