编辑:我希望从1到10的值以随机顺序出现在数组中。问题是,主循环保持循环,我无法得到结果。 请不要提供任何更好或某种先进的解决方案。它与解决方案无关,我正在努力学习,并希望了解此代码的问题所在。
import java.util.*;
public class CustomSorter
{
public static void main(String[] args)
{
int reqemlerinSayi = 10;
int[] esasSiyahi= new int[reqemlerinSayi];
Random random = new Random();
int reqem;
boolean toqqushur = false;
for (int i = 0; i < esasSiyahi.length; i++)
{
reqem = random.nextInt(reqemlerinSayi-1)+1;
for (int j = 0; j < esasSiyahi.length; j++)
{
if (esasSiyahi[j] == reqem)
toqqushur = true;
}
if (toqqushur)
i--;
else
esasSiyahi[i] = reqem;
toqqushur = false;
}
for (int i = 0; i < esasSiyahi.length; i++)
{
System.out.println(esasSiyahi[i]);
}
}
}
答案 0 :(得分:4)
当您尝试填充长度为random.nextInt(reqemlerinSayi-1)
的数组时,表达式reqemlerinSayi-1
将仅生成reqemlerinSayi
个值。删除-1
。
使用数字1 - 10填充ints
数组的更简单方法是:
List<Integer> list = new ArrayList<>();
for (int num = 1; num <= 10; num++) list.add(num);
Collections.shuffle(list);
int[] array = new int[list.size()];
for (int idx = 0; idx < list.size(); idx++) array[idx] = list.get(idx);
如果您对一系列Integers
做得很满意
List<Integer> list = new ArrayList<>();
for (int num = 1; num <= 10; num++) list.add(num);
Collections.shuffle(list);
Integer[] array = list.toArray(new Integer[list.size()]);
答案 1 :(得分:1)
您可以在Java 8中以下列方式执行此操作:
int lowInclusive = 5;
int highExcusive = 10;
List<Integer> randomList = new Random().ints(10, lowInclusive, highExcusive)
.boxed()
.collect(Collectors.toList());
这将执行以下操作:
IntStream
,由5个incusive和10个独占之间的10个随机整数组成。int
映射到装箱Integer
s。ArrayList<Integer>
Collectors.toList()
中的元素
醇>
在收到有关要求的更多输入后,您似乎想要所有唯一号码,请考虑以下事项:
int lowInclusive = 5;
int highExclusive = 10;
List<Integer> shuffledList = IntStream.range(lowInclusive, highExclusive)
.limit(10)
.boxed()
.collect(Collectors.toList());
Collections.shuffle(shuffledList);
这样做:
IntStream
。Integer
。shuffledList
。Collections.shuffle()
。答案 2 :(得分:0)
public class CustomSorter {
public static void main(String[] args) {
int min = 0;
int max = 100;
int sizeOfArray = 10;
int[] anArray = new int[sizeOfArray];
for(int x = 0; x < anArray.length(); x++) {
anArray[x] = min + (int)(Math.random() * ((max - min) + 1));
}
}
}
答案 3 :(得分:0)
对于范围内的random numbers
,制作函数会有很多帮助
然后你只需要迭代找到随机数的总数
class NewClass {
public static void main(String[] args) {
int startRange = 500;
int endRange = 600;
// create 50 random variable between 500 600
double random[]= new double[50];
for (int i = 0; i < random.length; i++) {
random[i] = findRandomNumber(startRange, endRange);
System.out.println(random[i]);
}
}
public static double findRandomNumber(double aStart, double aEnd) {
Random aRandom = new Random();
if (aStart > aEnd) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
//get the range, casting to long to avoid overflow problems
double range = aEnd - aStart + 1;
// compute a fraction of the range, 0 <= frac < range
double fraction = range * aRandom.nextDouble();
double randomNumber = (fraction + aStart);
return (randomNumber);
}
}
输出
579.0386837850086
578.4240926611131
531.3635427714237
.
.
.
538.4404995544475
510.7875548059947
582.3107995045374
557.5918170571055