Este exemplo traz poucas diferenças em relação
ao outro que postei antes deste.
O que caracteriza a principal diferença deste
pro outro é que aqui não usei arrayList,
e nem o método shuffle da classe Collections,
para embaralhamento dos números,
tudo foi feito usando arrays simples do tipo int
e o embaralhamento dos números foi feito através
da classe Random da biblioteca java.util.Random;
Acompanhe as imagens abaixo:
Veja abaixo o código do programa:
ao outro que postei antes deste.
O que caracteriza a principal diferença deste
pro outro é que aqui não usei arrayList,
e nem o método shuffle da classe Collections,
para embaralhamento dos números,
tudo foi feito usando arrays simples do tipo int
e o embaralhamento dos números foi feito através
da classe Random da biblioteca java.util.Random;
Acompanhe as imagens abaixo:
Veja abaixo o código do programa:
import java.util.Random;
public class PROJETO {
public static int tam = 30;
public static void Sorteia ( int vet [ ] ) {
int a = 0, i, temp, y;
J jht = new J ( );
//
/////////////////////////////
for ( i = 2; i < tam + 2; i++ ) {
vet [ a ] = i;
a++;
}
Random random = new Random ( );
for ( a = 0; a < tam; a++ ) {
y = random.nextInt ( tam );
temp = vet [ a ];
vet [ a ] = vet [ y ];
vet [ y ] = temp;
}
// ////////////////////////////
jht.textcolor ( jht.LIGHTRED );
jht.gotoxy ( ( short ) 24, ( short ) 3 );
System.out.printf ( "VETORES ALEATÓRIOS
- PRIMOS E NÃO PRIMOS" );
jht.textcolor ( jht.BLACK );
jht.gotoxy ( ( int ) 10, ( int ) 7 );
for ( i = 0; i < tam; i++ ) {
if ( i == 15 )
jht.gotoxy ( ( int ) 10, ( int ) 8 );
if ( i == 30 )
jht.gotoxy ( ( int ) 10, ( int ) 9 );
System.out.printf ( " %2d ", vet [ i ] );
}
jht.getche ( );
}
public static void Insere_Aleat ( int vet [ ] ) {
J jht = new J ( );
int vet_1 [ ] = new int [ tam ];
int vet_2 [ ] = new int [ tam ];
int a = 0, b = 0, c = 0, k = 0, i, z = 0;
int p, t = 0, u = 0, v = 0;
if ( k == tam && z >= 1 )
k = 0;
for ( i = 0; i < tam; i++ ) {
k++;
a = vet [ i ] - 2;
jht.textcolor ( jht.LIGHTRED );
jht.gotoxy ( ( short ) 23, ( short ) 10 );
System.out.printf ( "%d°", k + 0 );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( " Número
sorteado" );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( " %d ", vet [ a ] );
if ( k >= 1 && k <= tam ) {
jht.textcolor ( jht.LIGHTRED );
jht.gotoxy ( ( int ) 42, ( int ) 10 );
if ( k >= 1 || k <= tam ) {
for ( b = 1, p = 0; b <= vet [ a ]; b++ ) {
if ( vet [ a ] % b == 0 )
p++;
}
if ( p == 2 ) {
vet_1 [ t ] = vet [ a ];
t++;
u = t;
} else {
vet_2 [ c ] = vet [ a ];
c++;
v = c;
}
}
}
jht.gotoxy ( ( int ) 45, ( int ) 10 );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( " posição" );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( " %d", a );
jht.textcolor ( jht.LIGHTBLUE );
jht.gotoxy ( ( int ) 23, ( int ) 12 );
System.out.printf ( "O programa
está agora" );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( " copiando
" );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( "números" );
if ( k == tam ) {
jht.gotoxy ( ( short ) 23, ( short ) 15 );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( "Abaixo os números
primos entre " );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( "1" );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( " e" );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( " 32" );
jht.textcolor ( jht.BLACK );
jht.gotoxy ( ( short ) 22, ( short ) 17 );
for ( c = 0; c < u; c++ ) {
System.out.printf ( " %2d ", vet_1 [ c ] );
}
}
if ( k == tam ) {
jht.Apaga ( ( short ) 12, ( short ) 13, ( short ) 22,
( short ) 65 );
jht.gotoxy ( ( short ) 23, ( short ) 12 );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( "O
programa" );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( " copiou" );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( " com sucesso
" );
jht.gotoxy ( ( short ) 23, ( short ) 19 );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( "Abaixo os números
não primos entre " );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( "1" );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( " e" );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( " 32" );
jht.textcolor ( jht.BLACK );
jht.gotoxy ( ( short ) 22, ( short ) 21 );
for ( i = 0; i < v; i++ ) {
if ( i == 10 )
jht.gotoxy ( ( short ) 22, ( short ) 22 );
System.out.printf ( " %2d ", vet_2 [ i ] );
}
}
jht.textcolor ( jht.LIGHTRED );
jht.gotoxy ( ( short ) 30, ( short ) 24 );
System.out.printf ( "PRESSIONE
QUALQUER TECLA" );
jht.getche ( );
jht.Apaga ( ( short ) 10, ( short ) 11, ( short ) 54, ( short ) 57 );
if ( k == 30 )
break;
}
jht.Apaga ( ( short ) 12, ( short ) 13, ( short ) 22, ( short ) 65 );
}
public static void main ( String args [ ] ) {
J jht = new J ( );
jht.Moldura ( ( short ) 1, ( short ) 28, ( short ) 2, ( short ) 78 );
jht.title ( " " );
int z = 0, y = 0;
int vet [ ] = new int [ tam ];
do {
jht.cls ( );
jht.gotoxy ( ( short ) 23, ( short ) 12 );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( "Agora o
programa " );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( "copiará
" );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( "os números" );
jht.gotoxy ( ( short ) 24, ( short ) 13 );
System.out.printf ( "primos e não
primos em dois vetores" );
z++;
jht.textcolor ( jht.LIGHTRED );
jht.gotoxy ( ( short ) 29, ( short ) 5 );
System.out.printf ( "%dª", z + 0 );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( " Rodada
" );
jht.textcolor ( jht.LIGHTRED );
System.out.printf ( "%d ", tam );
jht.textcolor ( jht.LIGHTBLUE );
System.out.printf ( "números
gerados" );
Sorteia ( vet );
Insere_Aleat ( vet );
y++;
} while ( y < 3 );
jht.Apaga ( ( short ) 24, ( short ) 25, ( short ) 29, ( short ) 65 );
jht.Sleep ( ( short ) 1800 );
jht.textcolor ( jht.LIGHTBLUE );
jht.gotoxy ( ( short ) 23, ( short ) 24 );
System.out.printf ( "Por: " );
jht.textcolor ( jht.LIGHTMAGENTA );
System.out.printf ( "Samuel
Lima" );
jht.textcolor ( jht.BLACK );
jht.gotoxy ( ( short ) 23, ( short ) 25 );
System.out.printf ( "sa_sp10@hotmail.com" );
jht.Sleep ( ( short ) 1800 );
jht.textcolor ( jht.LIGHTRED );
jht.gotoxy ( ( short ) 36, ( short ) 27 );
System.out.printf ( "MUITO
OBRIGADO" );
jht.getche ( );
}
}
Nenhum comentário:
Postar um comentário
Observação: somente um membro deste blog pode postar um comentário.