Created
February 8, 2012 10:00
-
-
Save carlossaraiva/1767585 to your computer and use it in GitHub Desktop.
[ESD] Aula 01 - Homework 01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Autor: Carlos Eduardo Saraiva. | |
/*1 - Gerar um vetor com 10 números de 0 a 99. Pedir para o usuário digitar um número no intervalo definido (validar) | |
e verificar se o número aparece no vetor.Em caso afirmativo, verificar quantas vezes e em quais posições ele aparece. | |
*/ | |
import java.util.*; | |
public class HomeWork01 { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
int[] vetor = new int[10]; | |
int numeroUsuario = 0, totalEncontrado = 0; | |
Scanner entrada = new Scanner(System.in); | |
//Criar um vetor com números aleatórios | |
vetor = criarVetorRand(vetor); | |
//Entrada do usuário | |
System.out.println("Digite um número entre 0 e 99:"); | |
numeroUsuario = entrada.nextInt(); | |
//Validação | |
validar(numeroUsuario); | |
//Procurar no vetor o elemento digitado pelo usuário. | |
totalEncontrado = contarElementos(vetor,numeroUsuario); | |
//Define o resultado e sua exibição | |
if( totalEncontrado == 0) | |
{ | |
System.out.println("Valor não encontrado!"); | |
} | |
else | |
{ | |
exibirVetor(criarVetorDeIndices(vetor,numeroUsuario,totalEncontrado)); | |
} | |
//Apenas para o propósito de ver o array criado com números aleatórios. | |
debugVetor(vetor); | |
} | |
//Aqui começa as funções, quebrei em blocos para facilitar o compreendimento e estudo, no meu caso ajuda =P. | |
public static int[] criarVetorRand(int[] vetor) | |
{ | |
Random rand = new Random(); | |
for(int i = 0; i < 10; i++) | |
{ | |
vetor[i] = rand.nextInt(100); | |
} | |
return vetor; | |
} | |
public static void validar(int userNum){ | |
boolean valNum = false; | |
while(valNum == false) | |
{ | |
if(userNum >= 0 && userNum < 100) | |
{ | |
valNum = true; | |
} | |
else | |
{ | |
System.out.println("Número inválido! Animal!"); | |
} | |
} | |
} | |
public static int contarElementos(int[] vetor, int numero) | |
{ | |
int contador = 0; | |
for (int i = 0; i < vetor.length; i++) | |
{ | |
if (numero ==vetor[i] ) | |
{ | |
contador++; | |
} | |
} | |
return contador; | |
} | |
public static int[] criarVetorDeIndices(int[] vetor, int numeroProcura, int dimensao) | |
{ | |
int aux = 0; | |
int[] vetorIndice = new int[dimensao]; | |
for (int i = 0; i < vetor.length; i++) | |
{ | |
if (numeroProcura == vetor[i] ) | |
{ | |
vetorIndice[aux] = i; | |
aux++; | |
} | |
} | |
return vetorIndice; | |
} | |
private static void exibirVetor(int vetor[]) | |
{ | |
System.out.print("Valor encontrado no(s) índice(s): " ); | |
for (int i = 0; i < vetor.length; i++) | |
{ | |
if(i < vetor.length - 1) | |
{ | |
System.out.print(vetor[i] + ","); | |
} | |
else | |
{ | |
System.out.print(vetor[i] + "."); | |
} | |
} | |
} | |
public static void debugVetor(int array[]) | |
{ | |
System.out.print("\narray: ["); | |
for (int i = 0; i < array.length; i++) | |
{ | |
if(i < array.length - 1) | |
{ | |
System.out.print(array[i] + ", "); | |
} | |
else | |
{ | |
System.out.print(array[i] + "]"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment