把商品添加到数组里,然后对数组里的商品进行随机显示。
如何实现?

解决方案 »

  1.   


    int[] products = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };Random rnd = new Random();
    int index = rnd.Next(0, products.Length);
    int rndProducts = products[index];
    数库不同的话,也大同小异吧,呵呵。
      

  2.   

    random,可以rand很多数字,然后取对应数据。
      

  3.   

    int[] products = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };Random rnd = new Random();
    int index = rnd.Next(0, products.Length);
    int rndProducts = products[index];这个int[] products = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };里面的 123456789
    是下标还是ID?怎么用代码显示呢。
      

  4.   

    这个是指你的储存对象的数组,这个只是个示例啊要看你的数组是Array型的还是ArrayList型的,不过处理方式都一样啊。
      

  5.   


    //Product商品类型,list商品集合,getProducts获取商品集合的方法
    List<Product> list=getProducts();
    //rand随机
    Random rand = new Random(Convert.ToInt32(DateTime.Now.Ticks));
    //index=随机数=list索引值
    int index = rand.Next(0, list.Count - 1);
    //根据index获取该泛型集合对应的商品数据
    int i = list[index];
    不知道够详细了不
      

  6.   


    //Product商品类型,list商品集合,getProducts获取商品集合的方法
    List<Product> list=getProducts();
    //rand随机
    Random rand = new Random(Convert.ToInt32(DateTime.Now.Ticks));
    //index=随机数=list索引值
    int index = rand.Next(0, list.Count - 1);
    //根据index获取该泛型集合对应的商品数据
    Product pro = list[index];呵呵,最后一句写错啦,哈哈,应该是商品类型哈
      

  7.   


    //Product商品类型,list商品集合,getProducts获取商品集合的方法
    List<Product> list=getProducts();
    //rand随机
    Random rand = new Random(Convert.ToInt32(DateTime.Now.Ticks));
    while(list.Count > 0){
    //index=随机数=list索引值
    int index = rand.Next(0, list.Count - 1);
    //根据index获取该泛型集合对应的商品数据
    Product pro = list[index];
    list.remove(list[index]); //删除已显示的商品
    }小小修改下 `  防止重复商品的出现
      

  8.   

    Random rand = new Random(Convert.ToInt32(DateTime.Now.Ticks));
    报错 错误 55 “Random”方法没有采用“1”个参数的重载
    错误 56 “Random”并不包含“Next”的定义
      

  9.   

     Random rand = new Random((int)DateTime.Now.Ticks);不好意思啊,随机种子的类型装换弄错了,该这个,呵呵,读数据的时候习惯用Convert来转换了
      

  10.   

    阿。谢谢各位!其实一条sql语句就可以解决了! !select top 2 * from productInfo  order by NewId()