无法将int更改为string [英] Can't change int to string

查看:145
本文介绍了无法将int更改为string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我试图编写一个数独游戏,并希望使用文本框作为数字持有者。这就是为什么我想随机把这个号码放到文本框中但是我没有工作。



Today i was trying to code a sudoku game and wanted to use textboxes as number holders. That''s why i wanted to random and put that number in to the textbox but i didn''t work out.

TextBox[] dene = new TextBox[10];

for (int i = 0; i < 9; i++)
{
    Random b = new Random();
    int inta = b.Next(1,9);
    string a = inta.ToString();
    dene[i].Text = a;
}
_0 = dene[0];
_1 = dene[1];
_2 = dene[2];
_3 = dene[3];
_4 = dene[4];
_5 = dene[5];
_6 = dene[6];
_7 = dene[7];
_8 = dene[8];





这是我使用的代码,但它在dene [i] .Text = a时抛出错误;请帮助^^



This is the code i use but it throws an error during dene[i].Text=a; pls help ^^

推荐答案

因为 ThePhantomUpvoter 建议您必须初始化每个 dene 数组项,例如

As ThePhantomUpvoter suggested you have to initialize every dene array item, e.g.
for (int i = 0; i < 10; i++)
  dene[i] = new TextBox();


除了ThePhantomUpvoter所说的,在循环外移动你的Random构造函数,或者你几乎肯定会在所有文本框中得到相同的随机值。



In练习,你不想这样做,因为你不想要随机值:Suduko拼图的任何一行都需要1到9个包含 - 你的代码根本不能保证。
In addition to what ThePhantomUpvoter says, move your Random constructor outside the loop, or you will almost certainly get the same "random" value in all the text boxes.

In practice, you won''t want to do that anyway, as you do not want random values: any single line of a Suduko puzzle needs to be 1to 9 inclusive - your code does not guarantee that at all.


这篇关于无法将int更改为string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆