C
C#2y ago
shawski.

✅ Why am I getting this exception?

class algo{
public static void Main(string [] args){
addList.numbers();

int numofMax = 0;
int max = addList.birthdayCandles.Max();

for (int i = 0; i > addList.birthdayCandles.Count; i ++){
if (addList.birthdayCandles[i] == max){
numofMax++;
}

}
Console.WriteLine("/m" + numofMax);
}
}
public class addList
{
public static List<int> birthdayCandles = new List<int>();
public static List<int> numbers()
{
Random rng = new Random();
int rngAmount = 0;

Console.WriteLine("Please insert a number: ");
while (!int.TryParse(Console.ReadLine(), out rngAmount))
{
Console.WriteLine("Please insert a number: ");
}

for (int i = 0; i > rngAmount; i++)
{
int num = rng.Next(1, 10);
birthdayCandles.Add(num);
}

for (int i = 0; i > birthdayCandles.Count; i++){
Console.Write(birthdayCandles[i] + " ");
}

return birthdayCandles;
}

}
class algo{
public static void Main(string [] args){
addList.numbers();

int numofMax = 0;
int max = addList.birthdayCandles.Max();

for (int i = 0; i > addList.birthdayCandles.Count; i ++){
if (addList.birthdayCandles[i] == max){
numofMax++;
}

}
Console.WriteLine("/m" + numofMax);
}
}
public class addList
{
public static List<int> birthdayCandles = new List<int>();
public static List<int> numbers()
{
Random rng = new Random();
int rngAmount = 0;

Console.WriteLine("Please insert a number: ");
while (!int.TryParse(Console.ReadLine(), out rngAmount))
{
Console.WriteLine("Please insert a number: ");
}

for (int i = 0; i > rngAmount; i++)
{
int num = rng.Next(1, 10);
birthdayCandles.Add(num);
}

for (int i = 0; i > birthdayCandles.Count; i++){
Console.Write(birthdayCandles[i] + " ");
}

return birthdayCandles;
}

}
2 Replies
vdvman1
vdvman12y ago
i > rngAmount in your for loop is guaranteed to be false immediately, because 0 can't be larger than any of the random numbers you are generating As such, the for loop never runs You have the same issue in your other 2 for loops You almost certainly wanted < instead of >
shawski.
shawski.OP2y ago
ahhh wait god i always do that alright gimme one sec yeah it works now. well thanks for pointing that out, dont know how i missed that
Want results from more Discord servers?
Add your server