RANDOM EN JAVA
1
2
3
4
5 import java.io.*;
6 public class Random {
7 public static void main(String[] args) {
8 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
9 int n = 0;
10 int a =0;
11
12 String opcion = new String();
13 do{
14 System.out.println("d)igite rango");
15 System.out.println("s)alir");
16 try {
17 opcion = buffer.readLine();
18 if(opcion.equals("d")){
19 System.out.println("digite el intervalo de cero hasta:");
20 String cadena="";
21 cadena = buffer.readLine();
22 n = Integer.parseInt(cadena);
23 a =(int)(Math.random()* n);
24 System.out.println("el ganador es "+a);
25 }
26 } catch (IOException e) {
27 System.err.println("digite bien");
28 }
29 }while(!opcion.equals("s"));
30 }
31
32 }
1
2
3
4
5
6 import java.io.*;
7 public class Random {
8 public static void main(String[] args) {
9 BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in));
10 System.out.println("indique el numero de ganadores:");
11 String cad= new String();
12 int p=0;
13 try {
14 cad = buffer.readLine();
15 } catch (IOException e) {
16 System.err.println("LA DIGITACION ES INCORRECTA");
17 }
18 p=Integer.parseInt(cad);
19
20 System.out.println("Ingrese el rango de 0 hasta X");
21
22 String cad2 = new String();
23 int n=0;
24 try {
25 cad2=buffer.readLine();
26 } catch (IOException e) {
27 System.err.println("LA DIGITACION ES INCORRECTA");
28 }
29 n = Integer.parseInt(cad2);
30
31 int arr[]= new int[p];
32 int a = 0;
33 for(int y=0; y<p; y++){
34 a=(int)(Math.random()*n);
35 arr[y]=a;
36 if(arr[y]==a){
37 System.out.println(arr[y] );
38 }
39
40 }
41
42 }
43
44 }
CategoryJava | CategoryProgramacion