C#中如何随机取字符串中的文字
String[]present=String["红酒两瓶","马克杯一个","磐基礼卡100","磐基礼卡500","GUCCI包","BURBERRY香水"...
String[] present =String[ "红酒两瓶", "马克杯一个", "磐基礼卡100", "磐基礼卡500", "GUCCI包", "BURBERRY香水", "梵思诺毛巾", "OMEGA手表", "万宝龙背包", "施瓦洛斯奇水晶项链" ]; 这是我定义的字符串数组 怎样取得其中的任意一项
展开
3个回答
展开全部
C#中数组不是这样建的
String[] present =new String[]{ "红酒两瓶", "马克杯一个", "磐基礼卡100", "磐基礼卡500", "GUCCI包", "BURBERRY香水", "梵思诺毛巾", "OMEGA手表", "万宝龙背包", "施瓦洛斯奇水晶项链" };
取出可以使用随机数类Random r = new Random();
完整代码
String[] present =new String[]{ "红酒两瓶", "马克杯一个", "磐基礼卡100", "磐基礼卡500", "GUCCI包", "BURBERRY香水", "梵思诺毛巾", "OMEGA手表", "万宝龙背包", "施瓦洛斯奇水晶项链" };
Random r = new Random();
string str = present[r.Next(0,present.Length-1)];
String[] present =new String[]{ "红酒两瓶", "马克杯一个", "磐基礼卡100", "磐基礼卡500", "GUCCI包", "BURBERRY香水", "梵思诺毛巾", "OMEGA手表", "万宝龙背包", "施瓦洛斯奇水晶项链" };
取出可以使用随机数类Random r = new Random();
完整代码
String[] present =new String[]{ "红酒两瓶", "马克杯一个", "磐基礼卡100", "磐基礼卡500", "GUCCI包", "BURBERRY香水", "梵思诺毛巾", "OMEGA手表", "万宝龙背包", "施瓦洛斯奇水晶项链" };
Random r = new Random();
string str = present[r.Next(0,present.Length-1)];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询