您的位置:首页 > 编程语言 > Java开发

java经典算法_033随机生成不重复的数字

2012-12-03 19:59 405 查看
package wzs.arithmetics;

import java.util.Arrays;

// 1--10随机生成10个不重复的数字
public class Test_wzs33
{
public static void main(String[] args)
{
int count = 0; // 生成数字数量计数器
int temp; // 随机生成的数字
boolean isExist = false; // 判断数字是否存在 true存在,false不存在
int[] arr = new int[10];
while (count < 10)
{
temp = (int) (Math.random() * 10 + 1);
for (int i = 0; i < arr.length; i++)
{
if (temp == arr[i])
{
isExist = true;
break;
}
else
{
isExist = false;
}
}
if (!isExist)
{
arr[count] = temp;
count++;
}
}
System.out.println(Arrays.toString(arr));
}
}

输出结果:

[10, 7, 3, 6, 4, 8, 2, 1, 9, 5]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息