Fardar 0 / 0 / 0 Регистрация: 10.01.2019 Сообщений: 28 |
||||
1 |
||||
30.01.2019, 17:47. Показов 27637. Ответов 4 Метки нет (Все метки)
Всем привет.
__________________
0 |
Someone007 6270 / 3898 / 1567 Регистрация: 09.05.2015 Сообщений: 9,189 |
||||
30.01.2019, 17:49 |
2 |
|||
РешениеМассив не так выводится. Добавлено через 33 секунды
2 |
0 / 0 / 0 Регистрация: 10.01.2019 Сообщений: 28 |
|
30.01.2019, 18:16 [ТС] |
3 |
Большое спасибо!
0 |
Someone007 6270 / 3898 / 1567 Регистрация: 09.05.2015 Сообщений: 9,189 |
||||
30.01.2019, 18:31 |
4 |
|||
У вас выводится оригинальный массив.
1 |
Fardar 0 / 0 / 0 Регистрация: 10.01.2019 Сообщений: 28 |
||||
30.01.2019, 18:49 [ТС] |
5 |
|||
Да, это я уже заменил. Там все равно неверно получается. Но я уже решил эту проблему. Поменял немного функцию таким образом: перенес j и инкремент в условие цикла
0 |
IT_Exp Эксперт 87844 / 49110 / 22898 Регистрация: 17.06.2006 Сообщений: 92,604 |
30.01.2019, 18:49 |
5 |
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[])
My question is, how can I print out my 4 numbers (beside each other) in string format.
//EDIT: 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.
All fixed.
asked Jun 13, 2015 at 12:06
bsd ubsd u
771 silver badge6 bronze badges
1
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));
answered Jun 13, 2015 at 12:08
Grant WinneyGrant Winney
64.5k12 gold badges113 silver badges164 bronze badges
1
ticket1.ToString()
does not print the content of the array, only its type, because this is the way the ToString()
method is implemented on arrays.
You can fix this in several ways — for example, by using string.Join
method:
Console.WriteLine("{0}{1}", item.PadRight(20), string.Join(",", ticket1));
answered Jun 13, 2015 at 12:08
Because you are not writing your array elements, you are writing your array itself, that’s why ToString()
generates it’s full type name.
Change your ticket1.ToString()
to ticket1[i]
in your for
loop.
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[i]);
}
If you don’t want to print it inside your for
loop, then you can use String.Join
to concatenate all your elements in your array in a simple string like;
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), string.Join(",", ticket1));
answered Jun 13, 2015 at 12:08
Soner GönülSoner Gönül
96k102 gold badges205 silver badges356 bronze badges
2
Because when you call .ToString()
on an object you get the type of that object. For basic primitive (value) types this behavior is overridden to output the value. But for something like an array there’s no «default» string representation, so the behavior you’re seeing is the default.
You could wrap your data in an object and override .ToString()
on that object. If you have to output the values in many places in the code that would be the way to go so you only have to write the logic once. («Smart data structures and dumb code works a lot better than the other way around.» — Eric Raymond)
But if you only need to do it here then you can just output the values directly. Basically join the values as a string in whatever representation you want. For example, if they should be comma-separated:
Console.WriteLine(
"{0}{1}",
item.PadRight(20),
string.Join(",", ticket1));
answered Jun 13, 2015 at 12:11
DavidDavid
201k35 gold badges195 silver badges271 bronze badges
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[])
My question is, how can I print out my 4 numbers (beside each other) in string format.
//EDIT: 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.
All fixed.
asked Jun 13, 2015 at 12:06
bsd ubsd u
771 silver badge6 bronze badges
1
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));
answered Jun 13, 2015 at 12:08
Grant WinneyGrant Winney
64.5k12 gold badges113 silver badges164 bronze badges
1
ticket1.ToString()
does not print the content of the array, only its type, because this is the way the ToString()
method is implemented on arrays.
You can fix this in several ways — for example, by using string.Join
method:
Console.WriteLine("{0}{1}", item.PadRight(20), string.Join(",", ticket1));
answered Jun 13, 2015 at 12:08
Because you are not writing your array elements, you are writing your array itself, that’s why ToString()
generates it’s full type name.
Change your ticket1.ToString()
to ticket1[i]
in your for
loop.
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[i]);
}
If you don’t want to print it inside your for
loop, then you can use String.Join
to concatenate all your elements in your array in a simple string like;
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), string.Join(",", ticket1));
answered Jun 13, 2015 at 12:08
Soner GönülSoner Gönül
96k102 gold badges205 silver badges356 bronze badges
2
Because when you call .ToString()
on an object you get the type of that object. For basic primitive (value) types this behavior is overridden to output the value. But for something like an array there’s no «default» string representation, so the behavior you’re seeing is the default.
You could wrap your data in an object and override .ToString()
on that object. If you have to output the values in many places in the code that would be the way to go so you only have to write the logic once. («Smart data structures and dumb code works a lot better than the other way around.» — Eric Raymond)
But if you only need to do it here then you can just output the values directly. Basically join the values as a string in whatever representation you want. For example, if they should be comma-separated:
Console.WriteLine(
"{0}{1}",
item.PadRight(20),
string.Join(",", ticket1));
answered Jun 13, 2015 at 12:11
DavidDavid
201k35 gold badges195 silver badges271 bronze badges