Skip to content

Instantly share code, notes, and snippets.

@lukihardt
Last active March 21, 2019 08:31
Show Gist options
  • Select an option

  • Save lukihardt/3a6d4e8766ea58a943b9ab088908483c to your computer and use it in GitHub Desktop.

Select an option

Save lukihardt/3a6d4e8766ea58a943b9ab088908483c to your computer and use it in GitHub Desktop.
generate random int range[10-20],no repeat
package com.itheima.day07;
import java.util.Arrays;
import java.util.Random;
public class Test02 {
public static void main(String[] args) {
Random random = new Random();
int[] arrayInt = new int[5];
boolean flag = false;
int i = 0;
do {
//生成随机数
int temp = random.nextInt(11) + 10;
flag = false;
for (int j = 0; j <= i; j++){
//判断随机数和已生成的所有数组内元素是否相等
if( temp == arrayInt[j]){
flag = true;
break;
}
}
if(flag != true){
arrayInt[i] = temp;
i++;
}
} while (i < 5);
System.out.println(Arrays.toString(arrayInt));
}
}
@lukihardt
Copy link
Author

static变量行不通

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment