Tuesday, November 12, 2013

PL/SQL'de Sayısal Loto Oynamak

PL/SQL'de sayısal loto için 1-49 aralığında biri birinden farklı 6 rakam çeken ve sıralı olarak konsola yazan kodu aşağıda bulabilirsiniz:
1:  SET serveroutput ON  
2:  DECLARE  
3:  type t_lottery  
4:  IS  
5:   TABLE OF binary_integer;  
6:   v_numbers t_lottery := t_lottery(50,50,50,50,50,50);  
7:   v_candidate binary_integer;  
8:  BEGIN  
9:   FOR i IN 1..6  
10:   LOOP  
11:    DECLARE  
12:     v_found BOOLEAN := false ;  
13:    BEGIN  
14:     LOOP  
15:      v_candidate := ROUND(dbms_random.value(1,49),0);  
16:      v_found := false;  
17:      FOR n IN 1..i  
18:      LOOP  
19:       IF (v_numbers(n)=v_candidate) THEN  
20:        v_found := true ;  
21:       END IF;  
22:      END LOOP;  
23:      EXIT  
24:     WHEN NOT v_found;  
25:     END LOOP;  
26:    END;  
27:    DECLARE  
28:     v_temp v_candidate%type;  
29:    BEGIN  
30:     FOR j IN 1..i  
31:     LOOP  
32:      IF v_numbers(j) > v_candidate THEN  
33:       v_temp := v_candidate;  
34:       v_candidate := v_numbers(j);  
35:       v_numbers(j) := v_temp;  
36:      END IF;  
37:     END LOOP;  
38:    END;  
39:   END LOOP;  
40:   FOR i IN v_numbers.first..v_numbers.last  
41:   LOOP  
42:    dbms_output.put(v_numbers(i) || ' ');  
43:   END LOOP;  
44:   dbms_output.new_line;  
45:  END;  

Örnek konsol çıktıları aşağıda verilmiştir:

anonymous block completed
12 19 29 35 38 43

anonymous block completed
15 19 22 27 39 44

anonymous block completed
17 19 21 26 30 39

anonymous block completed
9 12 25 31 32 44

anonymous block completed
19 21 27 38 44 47

No comments:

Post a Comment