ビジネス ルールを適用する課題アクティビティの解決策を確認する

完了

次のコードは、前のユニットの課題に対して考えられる解答の 1 つです。

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}%.");
}

このコードは、ロジックを実装する方法に大きく依存するため、"1 つの考えられる解答" にすぎません。 課題のルールに従って適切な結果が得られており、2 つの if ステートメントを使用していれば、それで十分です。

正しくできたなら、おめでとうございます。 次のユニットの知識チェックに進んでください。

重要

この課題を完了できなかった場合は、先に進む前に、これまでのユニットを確認した方がよいと思われます。