domingo, 9 de abril de 2017

Math.random - Array multidimensional de inteiros

Estamos gerando 100 números aleatórios
usando o método Math.random, de no máximo
10, ou seja, de 0 a 10, e preenchemos
um array multidimensional de inteiros.
Agora vamos pesquisar por um elemento neste array.
Só que neste exemplo que trago agora,
os valores encontrados são marcados
no próprio array, pode observar que
os valores encontrados recebem a cor vermelha,
e o programa ainda informa o total de elementos
que foram encontrados.

Veja abaixo imagens do programa em execução:



Veja abaixo o código do programa:



import java.awt.Toolkit;
import java.util.Scanner;

public class PROJETO {
     static int         tam  = 10;
    // /////////////////////////////////////////////////////////////
     // Esta função não permite a entrada de caracteres
     public static int Aleatorios ( char c ) {
         int i;
         for ( i = 0; i < ( c ); i++ ) {
              if ( c < '0' || c > '9' )
                   return 0;
         }
         return 1;
     }
    // /////////////////////////////////////////////////////////////
     public static void main ( String [ ] args ) {
         J jht = new J ( );
         jht.textbackground ( jht.WHITE );
         jht.Moldura ( ( short ) 1, ( short ) 24, ( short ) 2, ( short ) 78 );
         jht.title ( " " );
         int i, j, n = 0, x = 0;
         int Vet [ ] [ ] = new int [ tam ] [ tam ];
         int Ve_t [ ] = new int [ 20 ];
         for ( i = 0; i < Vet.length; i++ ) {
              jht.gotoxy ( ( short ) 18, ( short ) i + 8 );
              for ( j = 0; j < Vet [ i ].length; j++ ) {
                   Vet [ i ] [ j ] = ( int ) ( Math.random ( ) * 10 );
                   System.out.printf ( " %3d ", Vet [ i ] [ j ] );
              }
         }
         jht.cls ( );
         jht.textbackground ( jht.WHITE );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( short ) 28, ( short ) 3 );
         System.out.printf ( "PESQUISANDO NÚMEROS EM MATRIZ " );
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( short ) 29, ( short ) 5 );
         System.out.printf ( "Imprimindo abaixo a Matriz " );
         jht.textcolor ( jht.BLACK );
         for ( i = 0; i < tam; i++ ) {
              jht.gotoxy ( ( short ) 18, ( short ) i + 8 );
              for ( j = 0; j < tam; j++ ) {
                   System.out.printf ( " %3d ", Vet [ i ] [ j ] );
              }
         }
         jht.Sleep ( ( short ) 800 );
         // /////////////////////////////////////////////////////////////
         do {
              // Entrada através de string
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( short ) 21, ( short ) 19 );
              System.out.printf ( "Digite um Número pra pesquisar ==> " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner ( System.in );
              String c = sc.next ( );
              // /////////////////////////////////////////////////////////////
              // temos abaixo uma conversão de String para char
              // dentro dos parâmetros da função Aleatorios
              if ( Aleatorios ( c.charAt ( 0 ) ) == 0 ) {
                   Toolkit.getDefaultToolkit ( ).beep ( );
                   jht.Apaga ( ( short ) 19, ( short ) 20, ( short ) 54,
                            ( short ) 59 );
                   continue;
              }
              // /////////////////////////////////////////////////////////////
              // Converte string para inteiro
              n = Integer.parseInt ( c );
              if ( n < 0 || n > 100 ) {
                   Toolkit.getDefaultToolkit ( ).beep ( );
                   jht.textcolor ( jht.LIGHTBLUE );
                   jht.gotoxy ( ( short ) 28, ( short ) 21 );
                   System.out.printf ( "O Número " );
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ( "%d", n );
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.printf ( " não existe na Matriz" );
                   jht.Sleep ( ( short ) 1800 );
                   jht.Apaga ( ( short ) 21, ( short ) 22, ( short ) 20,
                            ( short ) 65 );
                   jht.Apaga ( ( short ) 19, ( short ) 20, ( short ) 54,
                            ( short ) 59 );
                   continue;
              } else {
            for ( i = 0; i < tam; i++ ) {
                        jht.gotoxy ( ( short ) 18, ( short ) i + 8 );
                        for ( j = 0; j < tam; j++ ) {
                            if ( Vet [ i ] [ j ] == n ) {
                                 jht.textcolor ( jht.LIGHTRED );
                                 System.out.printf ( " %3d ", Vet [ i ] [ j ] );
                                 Ve_t [ x ] = Vet [ i ] [ j ];
                                 x++;
                            } else {
                                 jht.textcolor ( jht.BLACK );
                                 System.out.printf ( " %3d ", Vet [ i ] [ j ] );
                            }
                        }
                   }
              }
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 21, ( int ) 21 );
              jht.Sleep ( ( short ) 500 );
              System.out.printf ( "Encontramos " );
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ( "%d", x );
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf ( " números " );
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ( "%d", n );
              jht.getche ( );
              jht.Apaga ( ( short ) 21, ( short ) 22, ( short ) 20, ( short ) 68 );
              jht.Apaga ( ( short ) 19, ( short ) 20, ( short ) 14, ( short ) 60 );
              // /////////////////////////////////////////////////////////////
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 22, ( int ) 19 );
              jht.Sleep ( ( short ) 1800 );
              System.out.printf ( "Por: " );
              jht.textcolor ( jht.LIGHTMAGENTA );
              System.out.printf ( "Samuel Lima" );
              jht.textcolor ( jht.BLACK );
              jht.gotoxy ( ( int ) 22, ( int ) 20 );
              System.out.printf ( "sa_sp10@hotmail.com" );
              jht.Sleep ( ( short ) 1800 );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 34, ( int ) 22 );
              System.out.printf ( "MUITO OBRIGADO" );
              jht.getche ( );
              // /////////////////////////////////////////////////////////////
         } while ( true );
     }
}
 

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.