希望有人可以提供幫助。我正在自學C#,本章中的挑戰之一是要求我將每個月的天數存儲在一個名爲daysInMonth的數組中。當程序啓動時,我要求用戶輸入一個介於1到12之間的數字,然後吐出與該數字對應的月份中的天數。要求輸入並在陣列中打印該位置
我已經搜索了這個,但我什麼也沒有提出。大多數例子都與匹配/查找int或字符串與數組中的某些內容不是我想要的內容有關。我想要一些東西,以便如果用戶輸入數字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();
請提供您的代碼,然後有人可能會建議如何解決它。 –