C
C#2y ago
Elmishh

✅ not in OR in a list

trying to check if a number is inside a list if yes print that number if no add that number to the list
34 Replies
Elmishh
ElmishhOP2y ago
using System; class RNG { static void Main() { System.Random random = new System.Random(); List<int> triedlist = new List<int>(); while (true) { var rng1 = random.Next(50);
} } }
Anton
Anton2y ago
well do you know how ifs work?
Elmishh
ElmishhOP2y ago
not really not in C# at least
Elmishh
ElmishhOP2y ago
that doesnt really help me
Pobiega
Pobiega2y ago
I think it does. But please, expand on that line of thinking because here is what you said:
trying to check if a number is inside a list
note the if there
Elmishh
ElmishhOP2y ago
yeah ive tried using if but i dont know what from there there isnt an is in or if not command
Pobiega
Pobiega2y ago
there is, its just not written that way
Elmishh
ElmishhOP2y ago
and im trying to find that way
Pobiega
Pobiega2y ago
the syntax is if (condition) { actions-if-true } and your condition here is "does my list contain the number"
Thinker
Thinker2y ago
wait what well... you can't do "is in" with that
Elmishh
ElmishhOP2y ago
why is my code adding using without me putting it there
Pobiega
Pobiega2y ago
VS does that if you start writing code that needs it
Elmishh
ElmishhOP2y ago
using System; using System.ComponentModel; using System.ComponentModel.Design; class RNG { static void Main() { System.Random random = new System.Random(); List<int> triedlist = new List<int>(); var attempts = 0; while (true) { var rng1 = random.Next(500); if (triedlist.Contains(rng1) == false); { triedlist.Add(rng1); Console.WriteLine(rng1); attempts++; } if (triedlist.Contains(rng1) == true); { Console.WriteLine($"landed on {rng1} after {attempts} attempts"); break; } } } } well something is wrong considering it returns attempts as 1 and a random number each time no matter what what is wrong oh i get it gimme a second ?
Thinker
Thinker2y ago
if (x == true) is the same as if (x) and if (x == false) is the same as if (!x)
Elmishh
ElmishhOP2y ago
? well VS was shouting at me to put one there
Pobiega
Pobiega2y ago
no, it wasnt
Elmishh
ElmishhOP2y ago
well it works now
Pobiega
Pobiega2y ago
not with the semicolons there it really wont it will result in the blocks always running since they are no longer associated with the if
Elmishh
ElmishhOP2y ago
ah well theres still a slight problem im not sure how to fix it realy nevermind fixed it
Pobiega
Pobiega2y ago
its usually recommended that you share your solution when that happens
Elmishh
ElmishhOP2y ago
using System;
using System.ComponentModel;
using System.ComponentModel.Design;

class RNG
{
static void Main()
{
System.Random random = new System.Random();
List<int> triedlist = new List<int>();
var attempts = 0;

while (true)
{
var rng1 = random.Next(99999999);
Console.WriteLine(rng1);

if (triedlist.Contains(rng1))
{
Console.WriteLine($"landed on {rng1} after {attempts} attempts");
break;
}

if (!triedlist.Contains(rng1))
{
triedlist.Add(rng1);
attempts++;
}
}
}
}
using System;
using System.ComponentModel;
using System.ComponentModel.Design;

class RNG
{
static void Main()
{
System.Random random = new System.Random();
List<int> triedlist = new List<int>();
var attempts = 0;

while (true)
{
var rng1 = random.Next(99999999);
Console.WriteLine(rng1);

if (triedlist.Contains(rng1))
{
Console.WriteLine($"landed on {rng1} after {attempts} attempts");
break;
}

if (!triedlist.Contains(rng1))
{
triedlist.Add(rng1);
attempts++;
}
}
}
}
i switched placed between if (!triedlist.Contains(rng1)) and if (triedlist.Contains(rng1)) because it was adding the number and then telling me that it found it instantly after
Pobiega
Pobiega2y ago
two things
Elmishh
ElmishhOP2y ago
yeah?
Pobiega
Pobiega2y ago
one, you see when we post code, it gets highlighted and in a box? $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Pobiega
Pobiega2y ago
do this and your code will also be easy to read 🙂 two: your two ifs are mutually exclusive, only one can ever be true at once thats a fairly common thing, so there is actually syntax for it if (...) {} else {...} and this is better, as it will only evaluate list.contains once
Elmishh
ElmishhOP2y ago
using System;
using System.ComponentModel;
using System.ComponentModel.Design;

class RNG
{
static void Main()
{
System.Random random = new System.Random();
List<int> triedlist = new List<int>();
var attempts = 0;

while (true)
{
var rng1 = random.Next(99999999);
Console.WriteLine(rng1);

if (triedlist.Contains(rng1))
{
Console.WriteLine($"landed on {rng1} after {attempts} attempts");
break;
}

if (!triedlist.Contains(rng1))
{
triedlist.Add(rng1);
attempts++;
}
}
}
}
using System;
using System.ComponentModel;
using System.ComponentModel.Design;

class RNG
{
static void Main()
{
System.Random random = new System.Random();
List<int> triedlist = new List<int>();
var attempts = 0;

while (true)
{
var rng1 = random.Next(99999999);
Console.WriteLine(rng1);

if (triedlist.Contains(rng1))
{
Console.WriteLine($"landed on {rng1} after {attempts} attempts");
break;
}

if (!triedlist.Contains(rng1))
{
triedlist.Add(rng1);
attempts++;
}
}
}
}
ah thats how you put stuff in a box
Pobiega
Pobiega2y ago
if you put cs next to the opening `s you get highlighting
Elmishh
ElmishhOP2y ago
?
Pobiega
Pobiega2y ago
Elmishh
ElmishhOP2y ago
i never knew that existed
Pobiega
Pobiega2y ago
the cs tells discord you are writing C#
Elmishh
ElmishhOP2y ago
always get errors when i use else so i just dont anyways
Want results from more Discord servers?
Add your server