希望有人可以提供帮助。我正在自学 C#,本章中的一个挑战要求我将每个月的天数存储在一个名为 daysInMonth 的数组中。当程序启动时,我要求用户输入一个介于 1 到 12 之间的数字,然后吐出与该数字对应的月份中的天数。
我已经搜索过这个,但什么都没有。大多数示例都与在数组中匹配/查找 int 或字符串有关,这不是我想要的。我想要一些东西,如果用户输入数字 5,程序将打印出数组中第 5 个的任何内容。我知道这很容易,但我认为我的搜索一无所获,因为我不知道要搜索的正确术语。任何帮助,将不胜感激。
更新:
感谢 MAV,我得到了它的工作。发布程序的完整代码。
int[] daysInMonth = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
string[] monthNames = new string[12] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int myChoice;
Console.Write("Please enter a number: ");
myChoice = Int32.Parse(Console.ReadLine());
if (myChoice < 1)
{
Console.WriteLine("Sorry, the number {0} is too low. Please select a number between 1 and 12.", myChoice);
Console.Write("Please enter a number: ");
myChoice = Int32.Parse(Console.ReadLine());
}
else if (myChoice > 12)
{
Console.WriteLine("Sorry, the number {0} is too high. Please select a number between 1 and 12.", myChoice);
Console.Write("Please enter a number: ");
myChoice = Int32.Parse(Console.ReadLine());
}
int i = daysInMonth[myChoice - 1];
string m = monthNames[myChoice - 1];
Console.WriteLine("Thank you. You entered the number {0}.", myChoice);
Console.WriteLine("That number corresponds with the month of {0}.", m);
Console.WriteLine("There are {0} days in this month.", i);
Console.ReadLine();