随机数java1-9_随机数猜1-9的数字

这算是个小游戏

猜随机数

代码如下

package Day07; import java.lang.reflect.Array;

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

public class MatchingGame {

public static void main(String[] args) {

int[] arrOne = new int[5];

for(int i = 0; i < arrOne.length; i++) {

arrOne[i] = new Random().nextInt(10);

for(int j = 0; j < i; j++) {

if (arrOne[i] == arrOne[j]) {

j = 0;

arrOne[i] = new Random().nextInt(10);

}

}

}

int[] arrTwo = new int[5];

Scanner sc = new Scanner(System.in);

String str;

//System.out.println(Arrays.toString(arrOne));

boolean loopFlag = true;

while (loopFlag) {

for (int i = 0; i < arrOne.length; i++) {

System.out.println("Please input arrTwo[" + i + "]");

arrTwo[i] = sc.nextInt();

}

str = match(arrOne, arrTwo);

System.out.println(str);

loopFlag = !"a=5,b=0".equals(str);

}

}

/**

*

* @param arrOne 源数组

* @param arrTwo 与源数组匹配的数组

* @return

*/

public static String match(int[] arrOne, int[] arrTwo) {

int a = 0;

int b = 0;

for (int i = 0; i < arrOne.length; i++) {

for (int j = 0; j < arrTwo.length; j++) {

if (i == j && arrOne[i] == arrTwo[j]) {

a++;

} else if (i != j && arrOne[i] == arrTwo[j]) {

b++;

}

}

}

return "a=" + a + ",b=" + b;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Java中的Set和Random类来生成不重复的随机数序列。 具体实现方法如下: 1. 创建一个Set集合,用于存储随机数序列。 2. 使用Random类生成随机数,并将其添加到Set集合中,如果Set集合中已经存在该随机数,则重新生成随机数。 3. 重复步骤2,直到Set集合中存储的随机数个数达到指定的数量。 4. 将Set集合转换为数组,即可得到不重复的随机数序列。 以下是示例代码: ```java import java.util.HashSet; import java.util.Random; import java.util.Set; public class RandomDemo { public static void main(String[] args) { int n = 16; // 随机数数量 int min = 1; // 随机数最小值 int max = 16; // 随机数最大值 Set<Integer> set = new HashSet<>(); Random random = new Random(); while (set.size() < n) { int num = random.nextInt(max - min + 1) + min; set.add(num); } int[] arr = set.stream().mapToInt(Integer::intValue).toArray(); for (int i : arr) { System.out.print(i + " "); } } } ``` 输出结果类似于:7 15 12 5 4 14 1 11 6 3 16 8 13 2 9 10 ### 回答2: 实现Java 1-16的随机数不重复的方法有很多,下面是一种简单的方法: 1. 首先,创建一个长度为16的整型数组,用于存放取出的随机数,初始化为0。 2. 然后,使用Java的Random类生成一个随机数。 3. 判断该随机数是否已存在于数组中,如果不存在,则将该随机数存入数组中,并将数组中对应下标的值设置为该随机数。 4. 如果已存在于数组中,则重新生成一个随机数,并重复上述步骤。 5. 重复上述步骤,直到数组中存满16个不重复的随机数。 6. 最后,输出数组中的16个随机数。 示例代码如下: ```java import java.util.Random; public class RandomUniqueNumbers { public static void main(String[] args) { int[] numbers = new int[16]; int count = 0; Random random = new Random(); while (count < 16) { int randomNumber = random.nextInt(16) + 1; boolean isDuplicate = false; for (int i = 0; i < count; i++) { if (numbers[i] == randomNumber) { isDuplicate = true; break; } } if (!isDuplicate) { numbers[count] = randomNumber; count++; } } System.out.println("生成的随机数为:"); for (int i = 0; i < 16; i++) { System.out.print(numbers[i] + " "); } } } ``` 运行上述代码,会输出一个不重复的1-16范围内的随机数序列。 ### 回答3: 要实现Java中1到16的随机数不重复的功能,可以使用java.util包中的Random类和一个数组来实现。 首先,创建一个大小为16的整型数组,用来存放1到16这些数字。然后,利用Random类生成1到16之间的随机数,并将其与数组中的数字进行交换,以实现乱序排列。最后,根据需要获取随机数个数,从数组中取出对应个数的数字即可。 具体的步骤如下: 1. 导入java.util包中的Random类。 2. 创建一个大小为16的整型数组,用来存放1到16这些数字。 3. 使用Random类生成1到16之间的随机数,将其与数组中当前遍历的位置的数字进行交换,即将生成的随机数放入当前位置,并将当前位置之前的数字置为已经生成过的数字。 4. 循环执行上述步骤,直到遍历完整个数组。 5. 根据需要获取随机数个数,从数组中取出对应个数的数字即可。 通过以上步骤,即可实现1到16的随机数不重复的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值