从1到35去随机数,之后选择任意7位数 并且不重复 求大佬看看 哪里出了问题 现在代码会出现重复的数字

Lin_bai_ 2020-10-12 11:03:40

import java.util.Random;

public class Arbitrarily
{
public static void main(String[] args)
{
int [] tdarr= new int[35];
for (int i = 0;i < 35;i++ )
{
tdarr[i] =i+1;
}
Random r=new Random();//建立
for (int j = 0;j <= 7;j++)
{
int a = r.nextInt(tdarr[34]);
System.out.println(a);
remove(tdarr[34],a);
}
}
private static void remove(int i, int a)
{

}
}
创建数组,之后在数组里创建随机数,并且获取取值之后将他移除 在重写在数组里获取
...全文
1070 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
个人感觉你这个remove 函数中有问题,因为要在数组中移除,但是你函数中就没有把数组传进来,不知道你怎么移除的,我看你这个数组也是局部变量,函数获取不到,所以应该是没有移除的,你可以打印一下数组,是否移除了
Lin_bai_ 2020-10-12
  • 打赏
  • 举报
回复
好了我自己研究出来了


import java.util.ArrayList;
import java.util.Random;

public class Arbitrarily
{
    public static void main(String[] args)
    {
        ArrayList<Integer> aL=new ArrayList<Integer>();
        int [] tdarr= new int[35];
        for (int i = 0;i < 35;i++ )
        {
            tdarr[i] =i+1;
            aL.add(tdarr[i]);
        }

        for (int j = 1;j <=7;j++)
        {
            Random r=new Random();//建立
            int a = r.nextInt(aL.size());
            System.out.print(aL.get(a)+",");
            aL.remove(a);
        }
    }
}
大佬看看有什么地方可以优化下
Lin_bai_ 2020-10-12
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.Random;

public class Arbitrarily
{
    public static void main(String[] args)
    {
        ArrayList<Integer> aL=new ArrayList<Integer>();
        int [] tdarr= new int[3];
        for (int i = 0;i < 3;i++ )
        {
            tdarr[i] =i+1;
            aL.add(tdarr[i]);
        }

        for (int j = 1;j <= 2;j++)
        {
            Random r=new Random();//建立
            int a = r.nextInt(aL.size());
            System.out.println(a);
            aL.remove(a);
        }
    }
}
优化成这样也不行
七点二十喊我 2020-10-12
  • 打赏
  • 举报
回复
引用 13 楼 Lin_bai_ 的回复:
[quote=引用 9 楼 七点二十喊我 的回复:][quote=引用 6 楼 Lin_bai_ 的回复:][quote=引用 5 楼 七点二十喊我 的回复:]public class test { public static void main(String[] args) { HashSet<Integer> list = new HashSet(); Random a = new Random(); while (list.size() < 7){ int i = a.nextInt(35); list.add(i); } System.out.println(list); } } 这样子嗯
大佬 我像问下 这个随机数 怎么放进数组里呀[/quote] Integer[] objects = new Integer[list.size()]; Integer[] integers = list.toArray(objects); //或者直接强转 Integer[] objects1 = (Integer[]) list.toArray(); 数组转list list转数组 都是基础的[/quote] 可以写一个很简单的案例给我吗???大佬[/quote] public static void main(String[] args) { List<Integer> list = new ArrayList(); list.add(1); list.add(2); Integer[] l = new Integer[list.size()]; Integer[] obj1 = list.toArray(l); //或者直接强转 强转会抛类型转换异常哈 就别用了 // Integer[] obj2 =(Integer[]) list.toArray(); for (int i = 0; i < list.size(); i++) { System.out.print(obj1[0]); } // for (int i = 0; i < list.size(); i++) { // System.out.print(obj2[0]); // } }
Lin_bai_ 2020-10-12
  • 打赏
  • 举报
回复
引用 9 楼 七点二十喊我 的回复:
[quote=引用 6 楼 Lin_bai_ 的回复:][quote=引用 5 楼 七点二十喊我 的回复:]public class test { public static void main(String[] args) { HashSet<Integer> list = new HashSet(); Random a = new Random(); while (list.size() < 7){ int i = a.nextInt(35); list.add(i); } System.out.println(list); } } 这样子嗯
大佬 我像问下 这个随机数 怎么放进数组里呀[/quote] Integer[] objects = new Integer[list.size()]; Integer[] integers = list.toArray(objects); //或者直接强转 Integer[] objects1 = (Integer[]) list.toArray(); 数组转list list转数组 都是基础的[/quote] 可以写一个很简单的案例给我吗???大佬
Lin_bai_ 2020-10-12
  • 打赏
  • 举报
回复
引用 9 楼 七点二十喊我 的回复:
[quote=引用 6 楼 Lin_bai_ 的回复:][quote=引用 5 楼 七点二十喊我 的回复:]public class test { public static void main(String[] args) { HashSet<Integer> list = new HashSet(); Random a = new Random(); while (list.size() < 7){ int i = a.nextInt(35); list.add(i); } System.out.println(list); } } 这样子嗯
大佬 我像问下 这个随机数 怎么放进数组里呀[/quote] Integer[] objects = new Integer[list.size()]; Integer[] integers = list.toArray(objects); //或者直接强转 Integer[] objects1 = (Integer[]) list.toArray(); 数组转list list转数组 都是基础的[/quote] 我看的这本书好久就没有涉及这个
Lin_bai_ 2020-10-12
  • 打赏
  • 举报
回复
感谢感谢 麻烦各位大佬帮忙看看我别的帖子 感谢感谢
fengchuiqi_ 2020-10-12
  • 打赏
  • 举报
回复
引用 6 楼 Lin_bai_ 的回复:
大佬 我像问下 这个随机数 怎么放进数组里呀
        HashSet<Integer> list = new HashSet();
        ArrayList<Integer> arr = new ArrayList<>();
        Random a = new Random();
        while (list.size() < 7){
            int i = a.nextInt(35);
            if (i!=0) list.add(i);
        }
        arr.addAll(list);
        System.out.println(arr);
我做了一些改动,不过那位大佬是对的
七点二十喊我 2020-10-12
  • 打赏
  • 举报
回复
引用 6 楼 Lin_bai_ 的回复:
[quote=引用 5 楼 七点二十喊我 的回复:]public class test { public static void main(String[] args) { HashSet<Integer> list = new HashSet(); Random a = new Random(); while (list.size() < 7){ int i = a.nextInt(35); list.add(i); } System.out.println(list); } } 这样子嗯
大佬 我像问下 这个随机数 怎么放进数组里呀[/quote] Integer[] objects = new Integer[list.size()]; Integer[] integers = list.toArray(objects); //或者直接强转 Integer[] objects1 = (Integer[]) list.toArray(); 数组转list list转数组 都是基础的
黑色网袜yyds 2020-10-12
  • 打赏
  • 举报
回复
public class Test {
public static void main(String[] args) {
Set set=new HashSet();
while (set.size()<7){
int v = (int) (Math.random()*35+1);
set.add(v);
}
System.out.println(set);
}
}
还可以这样
fengchuiqi_ 2020-10-12
  • 打赏
  • 举报
回复
引用 5 楼 七点二十喊我 的回复:
public class test { public static void main(String[] args) { HashSet<Integer> list = new HashSet(); Random a = new Random(); while (list.size() < 7){ int i = a.nextInt(35); list.add(i); } System.out.println(list); } } 这样子嗯
有个BUG,这个方法会取到0
Lin_bai_ 2020-10-12
  • 打赏
  • 举报
回复
引用 5 楼 七点二十喊我 的回复:
public class test { public static void main(String[] args) { HashSet<Integer> list = new HashSet(); Random a = new Random(); while (list.size() < 7){ int i = a.nextInt(35); list.add(i); } System.out.println(list); } } 这样子嗯
大佬 我像问下 这个随机数 怎么放进数组里呀
七点二十喊我 2020-10-12
  • 打赏
  • 举报
回复
public class test { public static void main(String[] args) { HashSet<Integer> list = new HashSet(); Random a = new Random(); while (list.size() < 7){ int i = a.nextInt(35); list.add(i); } System.out.println(list); } } 这样子嗯
Lin_bai_ 2020-10-12
  • 打赏
  • 举报
回复
但是我移除了很多遍了 都没有相同的数字了

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧