初学者Java:在大小为7的数组中存储7个唯一的随机整数
我的任务是使用一个随机数生成器函数获得0到9之间的7个唯一整数,将它们存储在数组中,并显示输出 我试过下面的代码,但它没有给我7个唯一的整数。我仍然收到重复的值 非常感谢您的帮助,谢谢初学者Java:在大小为7的数组中存储7个唯一的随机整数,java,arrays,Java,Arrays,我的任务是使用一个随机数生成器函数获得0到9之间的7个唯一整数,将它们存储在数组中,并显示输出 我试过下面的代码,但它没有给我7个唯一的整数。我仍然收到重复的值 非常感谢您的帮助,谢谢 import java.util.Scanner; public class JavaProgramCh8Ex2 { //Global Scanner object to read input from the user: static Scanner keyboard = new Scanner(S
import java.util.Scanner;
public class JavaProgramCh8Ex2 {
//Global Scanner object to read input from the user:
static Scanner keyboard = new Scanner(System.in);
//Global variable to hold the size of the array:
final static int SIZE = 7;
//Main
public static void main(String[] args) {
//Populate the array with 7 numbers:
int [] sevenNumbers = new int [SIZE];
populateSevenNumbersArray(sevenNumbers);
//Display the numbers to the user:
displaySevenNumbers(sevenNumbers);
}
//Populate the numbers array with 7 random numbers:
public static void populateSevenNumbersArray (int [] numArray){
int maxElement;
for(maxElement = (SIZE - 1); maxElement > 0; maxElement--){
for (int i = 0; i <= (maxElement - 1); i++) {
numArray[i] = getRandomNumber(0, 9);
if(numArray[i] == numArray[i + 1]){
numArray[i + 1] = getRandomNumber(0, 9);
}
}
}
}
//Display the numbers to the user:
public static void displaySevenNumbers (int [] numArray){
for (int i = 0; i < numArray.length; i++) {
System.out.print(numArray[i] + " ");
}
}
//Get random numbers to populate the 7 numbers array:
public static int getRandomNumber(int low, int high){
return (int)(Math.random() * ((high + 1) - low)) + low;
}
}
import java.util.Scanner;
公共类JavaProgramCh8Ex2{
//要从用户读取输入的全局扫描仪对象:
静态扫描仪键盘=新扫描仪(System.in);
//保存数组大小的全局变量:
最终静态整数大小=7;
//主要
公共静态void main(字符串[]args){
//用7个数字填充阵列:
int[]七位数=新的int[大小];
填充偶数数组(七个);
//向用户显示数字:
显示七个数字(七个数字);
}
//用7个随机数填充数字数组:
公共静态void填充偶数数组(int[]numaray){
int-maxElement;
对于(maxElement=(大小-1);maxElement>0;maxElement--){
对于(int i=0;i,在该代码中
numArray[i] = getRandomNumber(0, 9);
if(numArray[i] == numArray[i + 1]){ // yes you retry here
numArray[i + 1] = getRandomNumber(0, 9); // but what about here
}
也许循环会更好
int num = getRandomNumber(0, 9);
while( isInArray(num){ // write a method to check
num = getRandomNumber(0, 9);
}
numArray[i] = num;
但实际上,当一个解决方案像
List<Integer> solution = new ArrayList<>();
for (int i = 0; i < 10; i++) { solution.add(i); }
Collections.shuffle(solution);
最后
for (int x : numArray) {
System.out.println(x);
}
输出
9
3
4
6
7
1
8
会更好可能有点过分,但我想尝试使用streams解决这个问题
public final static Random RANDOM = new Random();
/**
* Random unique integers from a given range [low, high)
* @param size - number of integers to take, must be less than or equal to high - low
* @param low - lower bound, inclusive
* @param high - upper bound, exclusive
* @return a list of unique, randomly chosen integers from the given range
* @throws IllegalArgumentException if size is greater than high - low.
*/
public static List<Integer> uniqueSample(int size, int low, int high) {
if (size > high - low) throw new IllegalArgumentException();
return Stream.generate(choice(low, high))
.distinct() // Discard duplicate
.limit(size) // Limit the size of the result.
.collect(Collectors.toList());
}
/**
* Return a random integer in range [low, high)
* @param low - lower bound, inclusive
* @param high - upper bound, exclusive
* @return a random integer between low and high (exclusive)
*/
private static Supplier<Integer> choice(int low, int high) {
return ()->low + RANDOM.nextInt(high - low);
}
public static void main(String [] args) {
uniqueSample(7, 0, 10).forEach(System.out::println);
}
public final static Random Random=new Random();
/**
*给定范围内的随机唯一整数[低,高)
*@param size-要获取的整数数,必须小于或等于高-低
*@param low-下限,包括
*@param高-上限,独占
*@返回给定范围内随机选择的唯一整数列表
*@如果大小大于高-低,则抛出IllegalArgumentException。
*/
公共静态列表uniqueSample(int-size、int-low、int-high){
如果(大小>高-低)抛出新的IllegalArgumentException();
返回流生成(选项(低、高))
.distinct()//放弃重复项
.limit(size)//限制结果的大小。
.collect(Collectors.toList());
}
/**
*返回[低,高]范围内的随机整数
*@param low-下限,包括
*@param高-上限,独占
*@返回一个介于低和高之间的随机整数(独占)
*/
私人静态供应商选择(整数低,整数高){
return()->low+RANDOM.nextInt(高-低);
}
公共静态void main(字符串[]args){
uniqueSample(7,0,10).forEach(System.out::println);
}
想法是一样的:你不断生成0到9之间的随机整数,直到得到一个你从未见过的整数,然后将其添加到结果中。当我们有7个这样的整数时停止。@ScaryWombat此问题与你指定的问题不同。请更改引用或重新打开。下面是一个解决方案,说明如何实现y您特别想要List solution=new ArrayList();for(int i=1;i)对您的代码进行注释。很好,您选择了一个常量,并将其命名为SIZE
。您可以随时更改大小。但不,您不能!您已经编写了“七”进入方法名和变量名!这和a一样糟糕。另外,不要编写自己的getRandomNumber
,只需使用。第二个不是解决方案。你需要将10个元素的列表洗牌,然后取7个。流很有趣。:)
public final static Random RANDOM = new Random();
/**
* Random unique integers from a given range [low, high)
* @param size - number of integers to take, must be less than or equal to high - low
* @param low - lower bound, inclusive
* @param high - upper bound, exclusive
* @return a list of unique, randomly chosen integers from the given range
* @throws IllegalArgumentException if size is greater than high - low.
*/
public static List<Integer> uniqueSample(int size, int low, int high) {
if (size > high - low) throw new IllegalArgumentException();
return Stream.generate(choice(low, high))
.distinct() // Discard duplicate
.limit(size) // Limit the size of the result.
.collect(Collectors.toList());
}
/**
* Return a random integer in range [low, high)
* @param low - lower bound, inclusive
* @param high - upper bound, exclusive
* @return a random integer between low and high (exclusive)
*/
private static Supplier<Integer> choice(int low, int high) {
return ()->low + RANDOM.nextInt(high - low);
}
public static void main(String [] args) {
uniqueSample(7, 0, 10).forEach(System.out::println);
}