0
Read a vector A with 12 elements. The vector must accept only 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 must show on the screen the numbers inserted in the vector.
Note: Use error handling/exception 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 methods try-catch
, should? I already check what is important in my while
.
It should not, exception should be an exception in the code, the only thing that should change is to use the
TryParse()
. Well, there are some things that could be a little better, but nothing very important.– Maniero
The way you say it, I would basically do this?
while(!TryParse....) {mensagem de ero?}
&#I hadn’t thought of that yet– Funny Dog
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero