日期:2014-05-16 浏览次数:20937 次
package net.liugeng.lottery; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Random; public class Main { public static void main(String[] args) { // 产生几组随机数,要求一组数中不能有重复的数字 System.out.print("请输入投注注数: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int nums = 0; try { nums = Integer.parseInt( br.readLine() ); } catch(IOException e) { System.out.println(e.getMessage()); } int[][] result; result = new int[nums][7]; Random rand = new Random(); for(int i = 0; i < nums; i ++) { for(int j = 0; j < 6; j ++) { int newNum = rand.nextInt(33) + 1; while(true) { boolean flag = true; for(int k = 0; k < j; k ++) { if(newNum == result[i][k]) { flag = false; } } if(flag == true) { break; } newNum = rand.nextInt(33) + 1; } result[i][j] = newNum; } result[i][6] = rand.nextInt(16) + 1; } for(int i = 0; i < nums; i ++) { System.out.print("第" + (i+1) + "组: 红色球为: "); for(int j = 0; j < 6; j ++) { System.out.print(result[i][j] + " "); } System.out.print("蓝色球为: " + result[i][6]); System.out.println(); } } }
------解决方案--------------------
declare
type tab is table of integer index by binary_integer;
v_tab tab;
v_num int;
v_i int := 0;
v_continue boolean := false;
begin
for i in 1 .. 7 loop
v_i := v_i + 1;
v_tab(v_i) := 0;
end loop;
v_i := 0;
loop
v_num := round(dbms_random.value(1, 36));
v_continue := false;
for i in 1 .. 6 loop
if v_tab(i) = v_num then
v_continue := true;
end if;
end loop;
if v_continue = false then
v_i := v_i + 1;
v_tab(v_i) := v_num;
if v_i < 7 then
dbms_output.put_line('red' || v_i || '=' || v_num);
else
dbms_output.put_line('blue' || '=' || v_num);
end if;
end if;
exit when v_i = 7;
end loop;
end;