标题:while与for循环排除时似乎没有作用
只看楼主
求知的胖子
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2016-12-22
结帖率:75%
已结贴  问题点数:20 回复次数:2 
while与for循环排除时似乎没有作用
    import java.util.*;
    public class examjava1{
                    /* *
                 * 这是main()方法.
                 * @param args传递至main()方法的参数
                */
            public static void main(String args[])   
                    {
                        int [] arr=new int [15];                    //定义长度为15的数组
                        System.out.print("arr:");
                            for(int i=0;i<arr.length;i++)                //for循环随机赋值0-99
                            {
                                arr[i]=(int)(Math.random()*100);
                                System.out.print("  "+arr[i]);
                            }
                            Arrays.sort(arr);
                            System.out.println("");
                            int j=0;
                            while(j==14){
                            for(int i=1;i<arr.length;i++)
                                {
                                       
                                        if(arr[j]==arr[i])
                                        {
                                            arr[i]=(int)(Math.random()*100);
                                        }
                                    
                                       
                                }
                                j++;
                                Arrays.sort(arr);
                                
                            }
                                for(int i=0;i<arr.length;i++)               
                            {
                                System.out.print(" "+arr[i]);
                            }
                           
                                    System.out.println("");
                                    System.out.println("s-min:"+arr[1]);
                                    System.out.println("certer:"+arr[7]);
                                    
                    }   
            }
        
搜索更多相关主题的帖子: import public 
2017-06-25 19:55
求知的胖子
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2016-12-22
得分:0 
要求是让我随机赋值,并且保证所有元素唯一。
2017-06-25 19:57
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:20 
程序代码:
Random random = new Random();
        int [] arr=new int [100];                 //定义长度为100的数组
        for(int i=0;i<arr.length;i++) {           //for循环赋值0-99
            arr[i]=i;
        }
        for(int i = 0; i < 15; i++) {
                int t = random.nextInt(100-i)+i;
                arr[i]=arr[i]+arr[t];
                arr[t]=arr[i]-arr[t];
                arr[i]=arr[i]-arr[t];
//                System.out.println(t);
//                System.out.println(Arrays.toString(arr));
        }
        Arrays.sort(arr,0,14);
        for(int i = 0; i < 15; i++) {
            System.out.print(" "+arr[i]);
        }
其实有 更简单的处理,但是过程不透明,这样写对处理流程更加深刻说不定呢

剑栈风樯各苦辛,别时冰雪到时春
2017-06-25 21:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-478924-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.114154 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved