-2

そこで、年利を計算するコンソール アプリケーションを作成しようとしています。しかし、預金年を割り当てると、どういうわけかクラッシュします。

ソースコードは以下です。

using System;
namespace week0502
{
   class Interest
   {
      static void Main(string[] args)
      {
         decimal p = 0; // principle
         decimal r = 1; // interest rate
         decimal t = 2; // time/years entered
         decimal i = 3; // interest
         decimal a = 4; // new amount
         // prompt for values
         Console.Write("Enter original deposit amount: ");
         p = Convert.ToDecimal(Console.ReadLine());
         Console.Write("Enter annual interest rate (10% as 10): ");
         r = Convert.ToDecimal(Console.ReadLine());
         Console.Write("Enter years to save this deposit amount: ");
         t = Convert.ToDecimal(Console.ReadLine());
         Console.WriteLine();
         // display headers
         Console.WriteLine("Year Rate Amount  Interest  New Amount");
         // calculate new amount on deposit after t amount of years
         for (int n = 1; n <= t; n++)
         {
               // calculations
               i = p * r;
               a = i + p;
               // display contents
               Console.WriteLine("{0, 4}{1, 4}{2, 10:C}{3, 5}{4, 10:C}", n, r, p, i, a);
            } // end for
      } // end main
   } // end class
} // end name
4

1 に答える 1

1

追加するのを忘れた場合は通常の結果を表示した後に閉じることを意味する場合Console.ReadLine()(アプリケーションを閉じる前にユーザーがEnterキーを押すのを待ちます)

using System;
namespace week0502
{
   class Interest
   {
      static void Main(string[] args)
      {
         decimal p = 0; // principle
         decimal r = 1; // interest rate
         decimal t = 2; // time/years entered
         decimal i = 3; // interest
         decimal a = 4; // new amount
         // prompt for values
         Console.Write("Enter original deposit amount: ");
         p = Convert.ToDecimal(Console.ReadLine());
         Console.Write("Enter annual interest rate (10% as 10): ");
         r = Convert.ToDecimal(Console.ReadLine());
         Console.Write("Enter years to save this deposit amount: ");
         t = Convert.ToDecimal(Console.ReadLine());
         Console.WriteLine();
         // display headers
         Console.WriteLine("Year Rate Amount  Interest  New Amount");
         // calculate new amount on deposit after t amount of years
         for (int n = 1; n <= t; n++)
         {
               // calculations
               i = p * r;
               a = i + p;
               // display contents
               Console.WriteLine("{0, 4}{1, 4}{2, 10:C}{3, 5}{4, 10:C}", n, r, p, i, a);
            } // end for
         Console.ReadLine(); //Add this to prevent application from closing
      } // end main
   } // end class
} 
于 2013-02-21T21:04:37.630 に答える