Read a vector A with 12 elements. The vector must only accept the input of values that are divisible by 2 or 3. The input of values in the vector must be validated by the program and not by the user. The program should display the numbers inserted in the vector.
Note: Use error / exception handling routines.
And my code is exactly this:
static void Main(string[] args)
{
string aux; // variavel auxiliar que ira receber o que o usuario digitar.
int valor;
int[] vetor = new int[12];
for (int i = 0; i < 12; i++)
{
aux = Console.ReadLine();
valor = int.Parse(aux);
Console.WriteLine("Digite um valor para o vetor");
while ((valor%2!=0) && (valor%3!=0))
{
Console.WriteLine("Apenas valores divisíveis por 2 e 3!");
aux = Console.ReadLine();
valor = int.Parse(aux);
}
vetor[i] = valor;
}
imprimeVetor(vetor);
Console.ReadKey();
}
static void imprimeVetor(int[] vetor)
{
for (int i = 0; i < 12; i++)
{
Console.Write(vetor[i] + " - ");
}
}
How to work with these error treatments. I have not used try-catch
methods, should I? I already check what's important in my while
.