如何让我的随机数字,没有重复一个println列表? [英] How do i make a println list of random numbers with no duplicates?

查看:125
本文介绍了如何让我的随机数字,没有重复一个println列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是我有办法可以使drawerList()方法,看看从drawerNumber()输出,并把它们添加到一个数组。我想输出到一个数组列表保存,以便找到重复的,并打印出所存储的值,并有可能使一个如果发现随机号码的重复,并标记这些产出为已绘制else语句,这是我第一次CS的学期,我在学习阶段,所以我觉得像什么,我试图做的是我的知识,我不知道该怎么做。

is there a way I can make the drawerList() method look at the output from the drawerNumber() and add them to an array. I want to store the outputs into an array list in order to find the duplicates and print out both the stored values and possibly make an if else statement that finds duplicates of the random numbers and mark those outputs as "already drawn" This is my first semester of CS and i am in the learning stages so i feel like what i'm trying to do is out of my knowledge and I'm not sure what to do.

import java.util.Scanner; 


import java.util.Random;

class RandomNumbers{
    public static void main(String [] args){

 System.out.println(drawerNumbers());
}

//drawerNumbers()-draws new numbers



public static int drawerNumbers(){
    Random rand = new Random();
    int num = rand.nextInt(75)+1;
    return num;
}

//  drawerList()- List of numbers drawn
 public static int drawerList(){ //Method.
  String p = Integer.toString(drawerList()); //Convert method into string in order to be put in array
int[] numbers = new int[p.length()]; // creates the array to store the values

//

在此之后不知道如何使输出上市到控制台,以及如何使程序找到重复的随机数。

After this not sure how to make the outputs be listed to the console and how to make the program find the duplicate random numbers.


  1. 标题

`##

推荐答案

看来你的意图是随机挑选1到75之间的数字不重复的数字。这是最容易被颠倒的问题做了随机排序的数字1-75,然后通过它们进行迭代:

It seems your intention is to randomly pick numbers between 1 and 75 without repeating a number. This is most easily done by inverting the problem to randomly ordering the numbers 1-75 then iterating through them:

List<Integer> nums = new ArrayList<Integer>(75);
for (int i = 1; i < 75; i++)
    nums.add(i);
Collections.shuffle(nums);

现在他们是随机的顺序,只需使用逐一:

Now they are in random order, just use them one by one:

for (Integer i : nums) {
    // use i
}

这篇关于如何让我的随机数字,没有重复一个println列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆