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.2y 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
More Posts
❔ Console.ReadKey() blockingHey there. I have this function that is called every frame: ```public void Update() { if ( !Co❔ Trouble Creating PowerShell CommandletI'm using visual studio 2022, dotnet 6.0. I installed the automation package using > NuGet\Install-P✅ Passing data from controller to viewHi guys hoping for some help here. I have an eCommerce wep app using ASP.NET MVC (7.0) and I want tHow to implement that when you click on the button the data that was sent with it will be displayedHow to implement that when you click on the button the data that was sent with it will be displayed❔ Visual Studio: How to hide tptrace output from Immediate Window during Test Explorer executions?Title says it all. Is there a way to hide the output messages from tptrace that get written to the I❔ Name "PostsService" does not exist in the current context```cs @using BlazorBookGroup.Data . . . private PostsService postsService = new(); protecte❔ System.AccessViolationException: 'Attempted to read or write protected memory.Even with <AllowUnsafeCodeBlocks> enabled and using unsafe { }, this error does not go away. And bef❔ namespace 'Models' does not exist in the namespace 'BlazorBookGroup'It is a subfolder though so what's the issue?❔ .Net Core API User.FindFirstValue(ClaimTypes.NameIdentifier) in every action?!Hi everyone I have the end point below ``` [HttpPost, Route("stores/new"), Authorize(policy: "client❔ Is it possible to change a project sdk from Microsoft.NET.Sdk to Microft.net.sdk.net?I'm completely new to c# and .NET so apologies if this doesn't make sense. If a project csproj file