C
C#14mo ago
Arone

❔ Array mess

I basically want this to return all of the numbers input into the array, sorted into smallest to largest
No description
13 Replies
Arone
AroneOP14mo ago
However it returns this
No description
Arone
AroneOP14mo ago
int count = 0; ArrayList raffleList = new ArrayList(); int[] raffleNumber = new int[5]; foreach (int number in raffleNumber) { Console.WriteLine("Please write a number from 1-60 that you would like to choose."); raffleNumber[count] = Convert.ToInt32(Console.ReadLine()); } raffleList.Add(raffleNumber); Array.Sort(raffleNumber); Console.WriteLine(raffleNumber); Console.ReadLine(); Help would be appreciated
Pobiega
Pobiega14mo ago
You can't just pass an array to console writeline Use string.Join to turn it into a string Also, use a for loop, not a foreach
Thinker
Thinker14mo ago
And god do not use ArrayList
langehk
langehk14mo ago
Maybe something like this?
int MAX_AMOUNT_OF_RAFFLE_NUMBERS = 5;
int MIN_NUMBER = 1;
int MAX_NUMBER = 60;
List<int> raffleList = new();


for (int i = 0; i < MAX_AMOUNT_OF_RAFFLE_NUMBERS; i++)
{
Console.WriteLine($"Please write a number from {MIN_NUMBER}-{MAX_NUMBER} that you would like to choose.");
var userInput = Console.ReadLine();

_ = int.TryParse(userInput, out int number);
if (number <= MAX_NUMBER && number >= MIN_NUMBER)
{
raffleList.Add(number);
}
else
{
Console.WriteLine("Incorrect value entered");
}
}

raffleList.Sort();

Console.WriteLine(String.Join(",", raffleList));
int MAX_AMOUNT_OF_RAFFLE_NUMBERS = 5;
int MIN_NUMBER = 1;
int MAX_NUMBER = 60;
List<int> raffleList = new();


for (int i = 0; i < MAX_AMOUNT_OF_RAFFLE_NUMBERS; i++)
{
Console.WriteLine($"Please write a number from {MIN_NUMBER}-{MAX_NUMBER} that you would like to choose.");
var userInput = Console.ReadLine();

_ = int.TryParse(userInput, out int number);
if (number <= MAX_NUMBER && number >= MIN_NUMBER)
{
raffleList.Add(number);
}
else
{
Console.WriteLine("Incorrect value entered");
}
}

raffleList.Sort();

Console.WriteLine(String.Join(",", raffleList));
Arone
AroneOP14mo ago
Its part of my course as like practice so i need to use it somewhere, but if it cant be used here then thats okay
Thinker
Thinker14mo ago
If it's part of your ancient course then sure it's fine, but you should never ever use it in any other circumstances.
Arone
AroneOP14mo ago
Yeah its an Alevel course soooo i need to at least learn how it works, but from ur ominous message i get that i shouldnt use it 😭
Pobiega
Pobiega14mo ago
Its a 20 year old class with literally not a single usecase left It still exists only for backward compatibility, and should never be used
Thinker
Thinker14mo ago
Yeah sorry I should like actually explain it. ArrayList was added all the way back in C# 1, back when the language didn't have this nifty thing called generics, but then in C# 2 generics were added along with the List<T> class, which is better in literally every single way. So tl;dr: ArrayList is really old and has no purpose for existing. sniped again Smadge
Pobiega
Pobiega14mo ago
Your text better thou You know I ❤️ you right?
Arone
AroneOP14mo ago
Okay i think i might just ignore it then Makes my life easier
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server