【发布时间】:2020-06-20 14:28:55
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter the Principal: ");
int principal = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the rate: ");
int rate = Convert.ToInt32(Console.ReadLine());
int a = rate / 100;
int b = a * principal;
int x = b + principal;
int[] sample = {};
Console.WriteLine("How long do you want the loop to run: ");
int loop = Convert.ToInt32(Console.ReadLine());
// StringBuilder sb = new StringBuilder();
for (int i = 0; i <= loop; i++)
{
sample[x] = x;
// sb.AppendLine(x.ToString());
b = a * x;
x = b + x;
}
Console.WriteLine(sample);
}
}
}
您好,我刚开始学习 C#,今天老师讨论的主题是数组。所以我决定创建一个简单的利息计算器,但它给了我
“System.IndexOutOfRangeException:索引超出了数组的范围。”
【问题讨论】:
-
仅供参考
Console.WriteLine(sample)不会给你有用的信息。而是使用Console.WriteLine(string.Join(", ", sample)); -
我完全按照你告诉我的做了,但它仍然显示同样的错误。