C
C#16mo 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
Elmishh16mo 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
Anton16mo ago
well do you know how ifs work?
Elmishh
Elmishh16mo ago
not really not in C# at least
Elmishh
Elmishh16mo ago
that doesnt really help me
Pobiega
Pobiega16mo 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
Elmishh16mo ago
yeah ive tried using if but i dont know what from there there isnt an is in or if not command
Pobiega
Pobiega16mo ago
there is, its just not written that way
Elmishh
Elmishh16mo ago
and im trying to find that way
Pobiega
Pobiega16mo ago
the syntax is if (condition) { actions-if-true } and your condition here is "does my list contain the number"
Thinker
Thinker16mo ago
wait what well... you can't do "is in" with that
Elmishh
Elmishh16mo ago
why is my code adding using without me putting it there
Pobiega
Pobiega16mo ago
VS does that if you start writing code that needs it
Elmishh
Elmishh16mo 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
Thinker16mo ago
if (x == true) is the same as if (x) and if (x == false) is the same as if (!x)
Elmishh
Elmishh16mo ago
? well VS was shouting at me to put one there
Pobiega
Pobiega16mo ago
no, it wasnt
Elmishh
Elmishh16mo ago
well it works now
Pobiega
Pobiega16mo 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
Elmishh16mo ago
ah well theres still a slight problem im not sure how to fix it realy nevermind fixed it
Pobiega
Pobiega16mo ago
its usually recommended that you share your solution when that happens
Elmishh
Elmishh16mo 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
Pobiega16mo ago
two things
Elmishh
Elmishh16mo ago
yeah?
Pobiega
Pobiega16mo ago
one, you see when we post code, it gets highlighted and in a box? $code
MODiX
MODiX16mo 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
Pobiega16mo 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
Elmishh16mo 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
Pobiega16mo ago
if you put cs next to the opening `s you get highlighting
Elmishh
Elmishh16mo ago
?
Pobiega
Pobiega16mo ago
Elmishh
Elmishh16mo ago
i never knew that existed
Pobiega
Pobiega16mo ago
the cs tells discord you are writing C#
Elmishh
Elmishh16mo ago
always get errors when i use else so i just dont anyways