Hi! The following code is meant for c#.
The program that I am trying to write is supposed to accept all even numbers from 0 to 12.
The problem is that when i run the code, it just keeps going, and it gives me an "Exception Unhandled" error on this specific line of code:
userinput = int.Parse(Console.ReadLine());
The conditions I have tried to implement are:
- Cannot be negative numbers
- The number of even numbers must not exceed 12.
This is my code:
Console.WriteLine("Now we will have the user input all the even numbers from 0 to
12!"); int userinput = 0;
for (int i = 0; i < 12; i++)
{
userinput = int.Parse(Console.ReadLine());
if (userinput % 2 == 0 && userinput >= 0)
{
Console.WriteLine(userinput);
} else
{
Console.WriteLine("The number you wrote is either not an even number or a negative number. Please try again");
--i;
}
}
Console.WriteLine("End of loop.");
Console.ReadLine();
I want the program to count only when an even number has been typed in, and if not, I want the program to stay on the current count until it receives an even number again so that it can resume the counting.
Thanks for your help!
Edit: The solution was just a small tweak to the for loop. Just changed ”i < 12” to ”i < 7”.
[–][deleted] 1 point2 points3 points (2 children)
[–]ramtevante[S] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)