Redian新闻
>
哪位大牛帮帮忙, 初学JAVA, 问题一道
avatar
哪位大牛帮帮忙, 初学JAVA, 问题一道# Java - 爪哇娇娃
h*t
1
Modify the code below so that the generate method does not create duplicate
numbers.You should add additional static methods, rather than put all the
code inside the existing loop.
下面是CODE:
import java.util.Random;
public class Part1
{
/**
* There are 5 numbers in each row
* (Similar to the 5 numbers under each letter in Bingo)
*/
public static final int TOTAL_NUMBERS = 5;
/**
* The smallest number is 1
*/
public static final int MIN_NUMBER
avatar
B*g
2
不太明白,难道不是要一个check dup的function?

duplicate

【在 h********t 的大作中提到】
: Modify the code below so that the generate method does not create duplicate
: numbers.You should add additional static methods, rather than put all the
: code inside the existing loop.
: 下面是CODE:
: import java.util.Random;
: public class Part1
: {
: /**
: * There are 5 numbers in each row
: * (Similar to the 5 numbers under each letter in Bingo)

avatar
A*o
3
应该不用,弄一个1到15序列,从里面随机挑,挪到新的位置就可以了。

【在 B*****g 的大作中提到】
: 不太明白,难道不是要一个check dup的function?
:
: duplicate

avatar
h*t
4
please give more detailed, thanks a lot!!!
avatar
z*e
5
/**
* This method populates an array a with numbers between MIN_NUMBER and
* MAX_NUMBER
*
* At present, it does not check for duplicates.
*/
public static void generate(int[] a) {
Set s = new HashSet();
Random r = new Random();
for (int i = 0; i < TOTAL_NUMBERS; i++) {
/**
* NB: r.nextInt returns a number that is >= 0 and < MAX_NUMBER
*
* So this statement generates a num
avatar
z*e
6
看了一下题目要求
感觉有些不太明白
只能在fixme那个地方加代码吗?
还是在main函数里面加一个静态方法?
如果是后者,检查出来有重复的数字,又该怎么办呢?
重新取?
avatar
h*t
7
Yes, you can only modify from "fixme"
Thank you so much, I appreciate your help!!
avatar
B*g
8
not insert array, reset indicator - 1?

【在 z****e 的大作中提到】
: 看了一下题目要求
: 感觉有些不太明白
: 只能在fixme那个地方加代码吗?
: 还是在main函数里面加一个静态方法?
: 如果是后者,检查出来有重复的数字,又该怎么办呢?
: 重新取?

avatar
A*o
9
另一种办法:
public static void generate(int[] a) {
Random r = new Random();
List list = new ArrayList(MAX_NUMBER);
for (int i = 0; i < MAX_NUMBER; i++) {
list.add(i + 1);
}
for (int i = 0; i < TOTAL_NUMBERS; i++) {
int size = list.size();
int index = r.nextInt(size);
a[i] = list.get(index);
list.remove(index);
}
}
avatar
w*d
10
Can't you keep a Set object as the instance field and store all the previous
random numbers in the set? And whenever, a new number is generated, check
whether it is already in the set or not. Sounds to me quite a
straightforward question.

duplicate

【在 h********t 的大作中提到】
: Modify the code below so that the generate method does not create duplicate
: numbers.You should add additional static methods, rather than put all the
: code inside the existing loop.
: 下面是CODE:
: import java.util.Random;
: public class Part1
: {
: /**
: * There are 5 numbers in each row
: * (Similar to the 5 numbers under each letter in Bingo)

avatar
c*t
11
This problem is a FAQ. Go to the programming board and read JHQ.
public static void generate (int[] a)
{
int size = MAX_NUMBER - MIN_NUMBER + 1;
int[] tmp = new int[size];
// initiate the numbers for swapping
for (int i = 0; i < size; ++i)
tmp[i] = MIN_NUMBER + i;
Random r = new Random();
// do the swapping
for (int i = 0; i < TOTAL_NUMBERS; ++i)
{
int swapIndex = r.nextInt (size - i);
a[i] = tmp[i + swapIndex];
tmp[i + swapIndex] = tmp[i];
}
}
avatar
l*g
12
我觉得zhaoce的方法已经可以了。
题目要求写一个静态方法调用来查看,那就每次去check这个
public static int checkUnqiue(int[] a, i)
{
List list = Arrays.asList(a);
Set set = new HashSet(list);
if(set.size(){
System.out.println("have duplicate");
i--;
}
return i;
}
然后再generate 的循环里面调用这个去控制i,这个不好的地方就是产生了好多没有用的内存对象,对erformance有影响。
不让改循环的话。那就把上面的代码的i--改成曲调用generate,然后把返回改成void。这样在GENERATE之后去掉用茶看是否有duplicate,如果有就重新生成,比较不好的是这样很难得到满意的数,毕竟是random。所以不过无论怎么做,循环都是要改的。我觉得zhaoce的方法已经
avatar
F*n
13
正解coconut已经给出了。

【在 l*******g 的大作中提到】
: 我觉得zhaoce的方法已经可以了。
: 题目要求写一个静态方法调用来查看,那就每次去check这个
: public static int checkUnqiue(int[] a, i)
: {
: List list = Arrays.asList(a);
: Set set = new HashSet(list);
: if(set.size(): {
: System.out.println("have duplicate");
: i--;

avatar
l*g
14
糊糊,coconut的方法真的不错,用过的就不用重复用了。

【在 F****n 的大作中提到】
: 正解coconut已经给出了。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。