Help

just trying to create a programme that adds up an even and an odd together
No description
14 Replies
SG97
SG974w ago
what's the meaning of the first if
ٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴ
even number divided by two leaves no remainders
SG97
SG974w ago
it's an empty if if (even % 2 == 0) or are you trying to check that the even is even and the odd is odd don't really like this style of if usage though but, what's the actual problem
sushiman
sushiman4w ago
do you want to check if the user wrote an odd and an even number? i don't really get what you tried to do
TizzyT
TizzyT4w ago
Console.WriteLine("Enter one even number and one odd number.");

int even = int.Parse(Console.ReadLine());
int odd = int.Parse(Console.ReadLine());

if (even % 2 == 0)
if (odd % 2 == 1)
Console.WriteLine(even + odd);
Console.WriteLine("Enter one even number and one odd number.");

int even = int.Parse(Console.ReadLine());
int odd = int.Parse(Console.ReadLine());

if (even % 2 == 0)
if (odd % 2 == 1)
Console.WriteLine(even + odd);
MutableString
MutableString4w ago
it would be wise to give an indication to the user if the validation of oddness/evenness fails
TizzyT
TizzyT4w ago
int even = RequestNumber("Enter an Even number:", i => i % 2 == 0, "Input not Even!");
int odd = RequestNumber("Enter an Odd number:", i => i % 2 == 0, "Input not Odd!");

Console.WriteLine(even + odd);

static int RequestNumber(string message, Func<int, bool>? constraint = null, string? constraint_message = null)
{
while (true)
{
Console.WriteLine(message);
string? input = Console.ReadLine();
if (int.TryParse(input, out int result))
{
if (constraint is null || constraint(result))
{
return result;
}
else if (constraint_message is not null)
{
Console.WriteLine(constraint_message);
continue;
}
}
Console.WriteLine("Invalid input.");
}
}
int even = RequestNumber("Enter an Even number:", i => i % 2 == 0, "Input not Even!");
int odd = RequestNumber("Enter an Odd number:", i => i % 2 == 0, "Input not Odd!");

Console.WriteLine(even + odd);

static int RequestNumber(string message, Func<int, bool>? constraint = null, string? constraint_message = null)
{
while (true)
{
Console.WriteLine(message);
string? input = Console.ReadLine();
if (int.TryParse(input, out int result))
{
if (constraint is null || constraint(result))
{
return result;
}
else if (constraint_message is not null)
{
Console.WriteLine(constraint_message);
continue;
}
}
Console.WriteLine("Invalid input.");
}
}
vl4do
vl4do4w ago
why are you overcomplicating things
TizzyT
TizzyT4w ago
because I made it simple originally
ٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴ
yeh im not this advanced thanks guys will try work on it idk what parse is shoulf check it out
sushiman
sushiman4w ago
seeing the level of the original problem, i think its a bit too advanced for that xd int.parse is converting string to int
ٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴ
i thought that was converttoint32
sushiman
sushiman3w ago
it works too ofc but basically the class Convert can convert string to any type, so basically if u have a custom class/struct it can convert string into it as for int.Parse is the int's own convert function
ٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴٴ
ah so there are specific comversions makes sende
Want results from more Discord servers?
Add your server