Java洗牌实现

作者: DataNerd | 来源:发表于2017-11-08 02:21 被阅读0次
    import java.util.*;
    
    public class LatteryDrawing{
        public static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            System.out.println("The number of you draw:");
    
            int number = scanner.nextInt();
    
            int[] numbers = new int[number];
            for(int i = 0;i < number;i ++){
                numbers[i] = i + 1;
            }
            System.out.println(Arrays.toString(numbers));
            
            int arrayLength = numbers.length;
            int randomIndex = numbers.length;
            
            int[] results = new int[arrayLength];
            
            while(randomIndex > 0){
                int r = (int)(Math.random() * randomIndex);
                results[arrayLength - randomIndex] = numbers[r];
                numbers[r] = numbers[randomIndex - 1];
                randomIndex --;
            }
    
            System.out.println(Arrays.toString(results));
    
            Arrays.sort(results);
            System.out.println(Arrays.toString(results));
        }
    }
    

    相关文章

      网友评论

        本文标题:Java洗牌实现

        本文链接:https://www.haomeiwen.com/subject/deasmxtx.html