sexta-feira, 3 de março de 2017

Contando caractere em array de char

A string é imprimida normalmente,
num array de char.
O programa pede para que se digite um
número entre 0 e o tamanho máximo da string,
Se digitar um número maior, recebe uma mensagem
de advertência, se o número digitado estiver
dentro do tamanho da string, o caractere
correspondente a posição será retornado.

Veja abaixo imagens do programa em execução:





Veja abaixo o código do programa:



import java.util.Scanner;

public class PROJETO {
     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, lenv;
         char ch [ ] = { 'S', 'a', 'm', 'u', 'e', 'l' };
         lenv = ch.length;
         while ( true ) {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( short ) 21, ( short ) 3 );
              System.out.printf ( "CONTANDO CARACTERES - SAIR [ -1 ] " );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( short ) 21, ( short ) 5 );
              System.out.printf ( "Nome da string ==> " );
              jht.textcolor ( jht.LIGHTRED );
              for ( i = 0; i < lenv; i++ )
              System.out.printf ( "%c", ch [ i ] );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( short ) 21, ( short ) 7 );
              System.out.printf ( "Total de caracteres ==> " );
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ( "%d", lenv );
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf ( " letras " );
              jht.textcolor ( jht.LIGHTBLUE );
              // ///////////////////////////////////////////////////
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( short ) 21, ( short ) 9 );
              System.out.printf ( "Digite um número entre " );
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ( "0" );
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf ( " e " );
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ( "%d ", lenv - 1 );
              jht.textcolor ( jht.BLACK );
              Scanner sc = new Scanner ( System.in );
              j = sc.nextInt ( );
              if ( j >= lenv ) {
                   jht.textcolor ( jht.LIGHTBLUE );
                   jht.gotoxy ( ( short ) 21, ( short ) 11 );
                   System.out.printf ( "Só digite números entre " );
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ( "0" );
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.printf ( " e " );
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ( "%d ", lenv - 1 );
                   jht.Sleep ( ( short ) 800 );
                   jht.textcolor ( jht.LIGHTRED );
                   jht.gotoxy ( ( int ) 29, ( int ) 21 );
                   System.out.printf ( "PRESSIONE QUALQUER TECLA" );
                   jht.getche ( );
                   main ( args );
              }
              // ////////////////////////////////////////////////////
              if ( j == -1 ) {
                   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 ( );
                   System.exit ( 0 );
              }
              // ////////////////////////////////////////////////////
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( short ) 21, ( short ) 11 );
              System.out.printf ( "Na posição " );
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ( "%d", j );
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf ( " está o caracter " );
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ( "%c", ch [ j ] );
              jht.textcolor ( jht.BLACK );
              jht.Sleep ( ( short ) 800 );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 29, ( int ) 21 );
              System.out.printf ( "PRESSIONE QUALQUER TECLA" );
              jht.getche ( );
              // ///////////////////////////////////////////////////
         }
     }
}



Nenhum comentário:

Postar um comentário

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