啊 !怎么能使他正确运行啊 想实现在一个string数组中随机提取一个名字放到textbox1中啊 新人啊欢迎各位大神来帮帮忙
求助啊 !!!如何能使他正确运行啊 想实现在一个string数组中随机提取一个名字放到textbox1中啊 新人求助啊欢迎各位大神来帮帮忙啊
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
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String[] sjz = new String[170] {"梦琪""书兰","雅琴","书瑶"};
Random r = new Random();
int x = Convert.ToInt32(sjz[r.Next(0, 170)]);
textBox1.Text = x.ToString();
}
}
}
------解决方案--------------------
------解决方案--------------------
int x = r.Next(0, 170);
textBox1.Text = sjz[x];
------解决方案--------------------
普通字符串是不能转换为int类型的,用Convert会报错
如果你确定字符串内容是数字可以用Convert或(int),或者Parse来转换
如果不确定可以用TryParse转换
------解决方案--------------------
实测,直接报错了
你要么String[] sjz = new String[170] ,然后循环赋值
要么String[] sjz = new String[] {"梦琪","书兰","雅琴","书瑶"};
让编译器自己算到底多长
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
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String[] sjz = new String[170] {"梦琪""书兰","雅琴","书瑶"};
Random r = new Random();
int x = Convert.ToInt32(sjz[r.Next(0, 170)]);
textBox1.Text = x.ToString();
}
}
}
------解决方案--------------------
String[] sjz = new String[] {"梦琪""书兰","雅琴","书瑶"};
Random r = new Random();
textBox1.Text = sjz[r.Next(sjz.lenght -1)];
------解决方案--------------------
int x = r.Next(0, 170);
textBox1.Text = sjz[x];
------解决方案--------------------
普通字符串是不能转换为int类型的,用Convert会报错
如果你确定字符串内容是数字可以用Convert或(int),或者Parse来转换
如果不确定可以用TryParse转换
------解决方案--------------------
实测,直接报错了
你要么String[] sjz = new String[170] ,然后循环赋值
要么String[] sjz = new String[] {"梦琪","书兰","雅琴","书瑶"};
让编译器自己算到底多长