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条)

报告相同问题?