【发布时间】:2021-06-20 01:52:23
【问题描述】:
我正在制作一个“游戏”,您可以在其中使用 Math.PI 输入 pi 的所有 17 位数字。我的想法是,您首先输入 Pi (3.14) 的前 3 位数字,然后继续。
using System;
public class Program
{
static void Main(string[] args)
{
double Pi = Math.PI;
string Pi_s = Pi.ToString();
char[] Pi_c = Pi_s.ToCharArray();
Console.WriteLine("Write the first 3 digits of pi");
string f = Console.ReadLine();
char[] e = f.ToCharArray();
int ind = 3;
while (ind < 16)
{
if(e[ind] == Pi_c[ind]) //here I get "Index was out of bounds with array"
{
Console.WriteLine("Congrats you got it right!");
ind = ind ++;
Console.WriteLine($"Now, type the first {ind} letters of pi");
}
else
{
Console.WriteLine("You got something wrong :(");
break;
}
}
}
}
我尝试更改 ind 的值,或者尝试在正确设置后将其添加到不同的值,但它不起作用。我认为问题在于您增加了ind 的值,但我不确定。
【问题讨论】:
-
提示:检查您的输入有多长。
-
P2i 是未声明的变量。应该是 Pi_s?
-
你让
ind一直到 15 来索引这些数组,但你从来没有检查过e的开始时间,也没有检查Pi_c的时间长度。 -
那么,我应该检查用户输入的数组的长度,如果小于3,我应该让他们再输入一次?
标签: c# arrays indexoutofboundsexception