为什么我要打印出"System.Int32 []"而不是数字数组? [英] Why am I printing out 'System.Int32[]' instead of my array of numbers?

查看:89
本文介绍了为什么我要打印出"System.Int32 []"而不是数字数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印我分配给特定数组的数字数组.我选择数字的算法包括选择一个不重复的随机数并将其存储在数组中.

I am trying to print out my array of numbers that I have assigned to a particular array. My algorithm for choosing numbers consists of choosing a random number that is not a duplicate and storing it inside the array.

真的很简单,但是我不知道为什么它会打印出此错误.

Pretty simple really, but I have no idea as to why it is printing out this error.

int[] ticket1 = new int[4];
for (int i = 0; i < 4; i++)
{
    int temp = rand.Next(43);
    while (ticket1.Contains(temp)) 
    {
        temp = rand.Next(43);
    }
    ticket1[i] = temp;
}
Console.WriteLine("{0}{1}", item.PadRight(20), ticket1.ToString());//ticket1 produces System.Int32[] instead of 4 numbers.
//I have changed this line to:
//Console.WriteLine("{0}{1}", item.PadRight(20), string.Join(",", ticket1));
//And it still doesn't work. the error remains. (System.Int32[])

我的问题是,如何以字符串格式打印我的4个数字(彼此相邻).

My question is, how can I print out my 4 numbers (beside each other) in string format.

//我发现了我的问题.我将我的ticket1放在一个foreach循环中,以某种方式未达到数组值,因此它打印出了System.Int32 [].

// I've found my problem. I am putting my ticket1 inside a foreach loop, it's somehow not reaching out to the array values and it therefore prints out System.Int32[] instead.

所有固定.

推荐答案

如果在这样的数组上调用ToString(),则只需获取类类型的全名即可.

If you call ToString() on an array like that, you simply get the full name of the type of class.

您可以通过几种方法对其进行修复.仅打印循环中的当前项目,或在循环外部一次打印每个项目:

You could fix it a few ways. Print only the current item inside the loop, or print each item one at a time outside of the loop:

Console.WriteLine("{0}{1}", item.PadRight(20), ticket1[0]);
Console.WriteLine("{0}{1}", item.PadRight(20), ticket1[1]);
// etc...

或在打印前展平"收藏集:

Or "flatten" the collection before printing:

Console.WriteLine("My numbers: ", String.Join(", ", ticket1));

这篇关于为什么我要打印出"System.Int32 []"而不是数字数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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