我很难找出我的代码有什么问题。我正在尝试在 C# 中创建一个控制台应用程序。该程序应该要求用户输入 3 个数字。所有的数字都必须大于 0。第一个数字应该是偶数,第二个应该是整数,第三个应该是奇数。我的语法似乎是正确的,但是,当我运行程序时,它似乎忽略了 if (userInput > 0) 部分。这是我的代码:
class Program
{
static void Main(string[] args)
{
try
{
int userInput;
Console.WriteLine("Please enter an even number.");
userInput = Convert.ToInt32(Console.ReadLine());
if (userInput > 0 && !isEven(userInput))
return;
Console.WriteLine("Please enter a whole number.");
userInput = Convert.ToInt32(Console.ReadLine());
if (userInput > 0)
Console.WriteLine("Please enter an odd number.");
userInput = Convert.ToInt32(Console.ReadLine());
if (userInput > 0 && isEven(userInput))
return;
Console.WriteLine("Congratulations!");
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
catch { }
}
static bool isEven(int value)
{
if (value % 2 == 0)
return true;
else
return false;
}
}
如果有人能告诉我如何正确测试这两种情况,我将永远感激不尽。谢谢。