我正在尝试将char数组转换为字符串,并将该字符串放入TextBlock中。 特定行'_textBl.Text = word;'使我的程序无法运行,这是完整的代码。
public sealed partial class MainPage : Page
{
int _currentIndex;
string _currentWord;
string[] _strArr = { "ant", "bee", "spider", "mosquito" };
int _difficulty = 1;
public MainPage()
{
Random rnd = new Random();
_currentIndex = rnd.Next(0, 5);
foreach (char c in _strArr[_currentIndex])
{
_currentWord = _strArr[_currentIndex];
string _temp = _currentWord;
char[] _wordArr = _currentWord.ToCharArray();
for (int i = 0; i < _wordArr.Length; i++)
{
_wordArr[i] = '_';
}
string word = new string(_wordArr);
_textBl.Text = word;
}
this.InitializeComponent();
}
}
}
答案 0 :(得分:0)
您可以使用(char)160
在字符串中插入空格,我简化了代码,如下所示。
请注意,实施的_strArr
只有4个项目,如果您输入rnd.Next(0,5),将导致超出范围的访问。
int _currentIndex;
string _currentWord;
string[] _strArr = { "ant", "bee", "spider", "mosquito" };
int _difficulty = 1;
Random rnd = new Random();
_currentIndex = rnd.Next(0, 4);
foreach (char c in _strArr[_currentIndex])
{
_currentWord = _strArr[_currentIndex];
string rest = string.Empty;
foreach (var item in _currentWord)
{
rest += "_" + (char)160;
}
MyTb.Text = rest;
}