檢閱套用商務規則挑戰活動的解決方案
下列程式碼是先前單元中挑戰的其中一個可能解決方案。
Random random = new Random();
int daysUntilExpiration = random.Next(12);
int discountPercentage = 0;
if (daysUntilExpiration == 0)
{
Console.WriteLine("Your subscription has expired.");
}
else if (daysUntilExpiration == 1)
{
Console.WriteLine("Your subscription expires within a day!");
discountPercentage = 20;
}
else if (daysUntilExpiration <= 5)
{
Console.WriteLine($"Your subscription expires in {daysUntilExpiration} days.");
discountPercentage = 10;
}
else if (daysUntilExpiration <= 10)
{
Console.WriteLine("Your subscription will expire soon. Renew now!");
}
if (discountPercentage > 0)
{
Console.WriteLine($"Renew now and save {discountPercentage}%.");
}
此程式碼之所以只是「一個可能的解決方案」,是因為您決定實作邏輯的方式會帶來很大的影響。 只要您能依照挑戰的每項規則取得正確的結果,而且使用兩個 if
陳述式,則代表您表現得很好!
如果成功,恭喜您! 請繼續進行下一個單元中的知識檢定。
重要
如果您無法順利完成此挑戰,您應該在繼續之前先複習上一個單元中的內容。