Advertisement
LightProgrammer000

Valores sem Repetição [JOptionPane]

Jan 3rd, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package REVISÃO;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class Vetores_1
  6. {
  7.     public static void main ( String args [] )
  8.     {
  9.         // ATRIBUTOS //
  10.         int x; // Índice da Matriz
  11.         int i; // Índice do Valor
  12.         int vet [] = new int [5]; // Vetor
  13.              
  14.         String ac = ""; // Acumulador
  15.         int b = 0; // Controlador de erro
  16.        
  17.         // Percorrendo a sequência a ser digitada
  18.         for( x = 0; x < 5; x ++ )
  19.         {
  20.             vet[x] = Integer.parseInt( JOptionPane.showInputDialog ( null, " Digite o " + ( x + 1 ) + " º Valor ") );
  21.            
  22.             if ( x != 0 )
  23.             {
  24.                 // Conferindo 1 a 1 entre o valor digitado e a posição que se encontra: Vet[2] = Vet[0] e Vet[1];
  25.                 for ( i = 0; i < x ; i ++ )
  26.                 {
  27.                     if ( vet[x] == vet[i] )
  28.                     {
  29.                         b = 1; // Ganha "1", caso o valor se repita
  30.                        
  31.                         JOptionPane.showMessageDialog( null, " NÚMERO REPETIDO, DIGITE NOVAMENTE ");
  32.                         x--;
  33.                        
  34.                         break; // Quebra o laço, saindo imediatamente do "for"
  35.                     }
  36.                    
  37.                     else
  38.                     {
  39.                         b = 0; // Ganha um "0" se o valor não se repetir
  40.                     }
  41.                 }
  42.                 // Somando o "1" ao "i"; i = i + 1;
  43.             }
  44.            
  45.             if ( b == 0 )
  46.             {
  47.                 ac += "\n" + vet[x]; // Só passa ao "ac", que não tem erro;
  48.             }
  49.            
  50.             // Somando o "1" ao "x"; x = x + 1;
  51.         }
  52.            
  53.         JOptionPane.showMessageDialog( null, ac );
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement