core Java 关于这个问题有个例子是关于抽奖的: 如果有兴趣楼主可以看看 import java.util.*;public class LotteryDrawing { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("How many numbers do you need to draw? "); int k = in.nextInt(); System.out.print("What is the highest number you can draw? "); int n = in.nextInt(); // fill an array with numbers 1 2 3 . . . n int[] numbers = new int[n]; for (int i = 0; i < numbers.length; i++) numbers[i] = i + 1; // draw k numbers and put them into a second array int[] result = new int[k]; for (int i = 0; i < result.length; i++) { // make a random index between 0 and n - 1 int r = (int) (Math.random() * n); // pick the element at the random location result[i] = numbers[r]; // move the last element into the random location numbers[r] = numbers[n - 1]; n--; } // print the sorted array Arrays.sort(result); System.out.println("Bet the following combination. It'll make you rich!"); for (int r : result) System.out.println(r); } }
//思路是定义另外一个长度一样的判定数组!
int[] temp=new int[5]{0,0,0,0,0};
//当取出这个数的时候,把值从0改为1
//下次再取的时候!检验该索引下的temp数组的该元素是否为0
//为0则取出,不为0就重新随机取数!
int lengths=0;
Random r=new Random();
while(lengths!=num.length){
int a=r.next(num.length);
if(temp[a]==0){
System.out.println(num[a]);
lengths++;
}
}
//思路是定义另外一个长度一样的判定数组!
int[] temp=new int[5]{0,0,0,0,0};
//当取出这个数的时候,把值从0改为1
//下次再取的时候!检验该索引下的temp数组的该元素是否为0
//为0则取出,不为0就重新随机取数!
int lengths=0;
Random r=new Random();
while(lengths!=num.length){
int a=r.next(num.length);
if(temp[a]==0){
System.out.println(num[a]);
lengths++;
temp[a]=1;
}
}上面漏写了取完之后要修改0为1!
假如,数组为a,长度为n,第一取的数组元素下标为“r”,那么:第一次取完后,将a【r】=a【n-1】;下次再取元素的时候,不取最后一个元素,即只取到a【n-1】,这样就不会重复取同一下标对应的元素以后以此类推
我觉得这是不错的思路,请指正~
假如,数组为a,长度为n,第一取的数组元素下标为“r”, 那么:第一次取完后,将a【r】=a【n-1】; 下次再取元素的时候,不取最后一个元素,即只取到a【n-2】,这样就不会重复取同一下标对应的元素 以后以此类推
我觉得这是不错的思路,请指正~更正一下
import java.util.*;public class LotteryDrawing
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in); System.out.print("How many numbers do you need to draw? ");
int k = in.nextInt(); System.out.print("What is the highest number you can draw? ");
int n = in.nextInt(); // fill an array with numbers 1 2 3 . . . n
int[] numbers = new int[n];
for (int i = 0; i < numbers.length; i++)
numbers[i] = i + 1; // draw k numbers and put them into a second array
int[] result = new int[k];
for (int i = 0; i < result.length; i++)
{
// make a random index between 0 and n - 1
int r = (int) (Math.random() * n); // pick the element at the random location
result[i] = numbers[r]; // move the last element into the random location
numbers[r] = numbers[n - 1];
n--;
} // print the sorted array
Arrays.sort(result);
System.out.println("Bet the following combination. It'll make you rich!");
for (int r : result)
System.out.println(r);
}
}
// 1~10的随机10个不同的数
int a[] = new int[10];
a[0] = (int) (Math.random() * 10) + 1;
for (int i = 1; i < a.length;) {
int t = (int) (Math.random() * 10) + 1; // 1~29
boolean f = true;
for (int j = 0; j < i; j++) {
if (t == a[j]) {
f = false;
break;
}
}
if (f) {
a[i] = t;
i++;
} } // 结果显示
for (int i = 0; i < a.length ; i++) {
System.out.print(a[i] + "\t");
}
}}
j的范围在0~i(不包含);一个一个取,取了后比较j中有没有与i重复的,如有重复:
i--;break;(跳出内循环,保证i不加)