Last active
March 21, 2019 08:31
-
-
Save lukihardt/3a6d4e8766ea58a943b9ab088908483c to your computer and use it in GitHub Desktop.
generate random int range[10-20],no repeat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
static变量行不通