标题:在类里面泛型字符串数组打印出来就正常,回到调用它的主程序里面打印,这个 ...
只看楼主
show147
Rank: 2
等 级:论坛游民
威 望:3
帖 子:40
专家分:81
注 册:2020-3-14
结帖率:100%
已结贴  问题点数:20 回复次数:2 
在类里面泛型字符串数组打印出来就正常,回到调用它的主程序里面打印,这个泛型数就自动变成一样的啦?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        public List<String[]> SelectNums = new List<string[]>();
        private string[] nums = new string[7];
        Random ra = new Random();
        public void Sjs(int item)
        {
            Random ra = new Random();
            for (int i = 0; i < item; i++)
            {
                for (int z = 0; z < 7; z++)
                {
                    this.nums[z] = ra.Next(10).ToString();
                }
                this.SelectNums.Add(nums);
                 for (int z = 0; z < 7; z++)
                 {
                    Console.Write(this.SelectNums[i][z]+"  ");
                 }
                 Console.ReadLine();
            }
        }
        static void Main(string[] args)
        {
            Program li = new Program();
            li.Sjs(2);
             for (int i = 0; i < 2; i++)
            {
                Console.Write("返回   ");
                for (int z = 0; z < 7; z++)
                {
                    Console.Write(li.SelectNums[i][z] + "  ");  
                }
                 Console.ReadLine();
            }
           
        }
    }
}
搜索更多相关主题的帖子: int for System 打印 泛型 
2021-07-12 16:22
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:185
帖 子:1404
专家分:8479
注 册:2010-3-16
得分:20 
private string[] nums= new string[7];
这个就new了一次,后面的循环中都是改变num的值,你把sjs中的输出单独提出来就会发现添加的2次num值是相同的。

更改:
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        public List<String[]> SelectNums = new List<string[]>();
        private string[] nums;// = new string[7];
        Random ra = new Random();
        public void Sjs(int item)
        {            
            //Random ra = new Random();
            for (int i = 0; i < item; i++)
            {
                nums = new string[7];//生成新的数组
                for (int z = 0; z < 7; z++)
                {
                    this.nums[z] = ra.Next(10).ToString();
                }
                this.SelectNums.Add(nums);
                 
            }
            for (int i = 0; i < item; i++)
            {
                 for (int z = 0; z < 7; z++)
                 {
                    Console.Write(this.SelectNums[i][z]+"  ");
                 }
                 Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            Program li = new Program();
            li.Sjs(2);
             for (int i = 0; i < 2; i++)
            {
                Console.Write("返回   ");
                for (int z = 0; z < 7; z++)
                {
                    Console.Write(li.SelectNums[i][z] + "  ");  
                }
                 Console.WriteLine();
            }
           
        }
    }
}
2021-07-12 17:17
show147
Rank: 2
等 级:论坛游民
威 望:3
帖 子:40
专家分:81
注 册:2020-3-14
得分:0 
非常感谢!!
2021-07-13 11:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506226-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.108429 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved