我对 C# 还很陌生,出于某种原因,我被抛出一个 IndexOutOfRangeException 用于边界为 0 和 0 的子字符串。
我认为这不是我的范围的问题,因为我已经测试以确保在使用它的地方定义了所有内容。
我正在尝试制作一个非常简单的字谜生成器:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string[] d = { "Apple", "Bass", "Cat", "Dog", "Ear", "Flamingo", "Gear", "Hat", "Infidel", "Jackrabbit", "Kangaroo", "Lathargic", "Monkey", "Nude", "Ozzymandis", "Python", "Queen", "Rat", "Sarcastic", "Tungston", "Urine", "Virginia", "Wool", "Xylophone", "Yo-yo", "Zebra", " "};
string var;
int len = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var = textBox2.Text;
//textBox1.Text = d[2];
for (int y = 0; y <= var.Length; y++)
{
for (int x = 0; x <= d.Length; x++)
{
if (d[x].Substring(0, 0).ToUpper() == var.Substring(len, len).ToUpper())
{
textBox1.Text = textBox1.Text + "\n" + d[x];
len = len + 1;
}
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}