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