✅ comparing variable to a number
im very very very new to C# and im trying to recode some basic things i made in python but i cant seem to figure out how to compare a variable like B to a number like 60
im trying to do
if (b) == 60;
but its giving me:
Invalid expression term '=='
71 Replies
how do i compare them if its not ==?
or am i doing something wrong
if (b == 60)
Angius#1586
REPL Result: Success
Console Output
Compile: 593.712ms | Execution: 84.465ms | React with ❌ to remove this embed.
also mind if i ask how i make C# wait 1 second?
await Task.Delay(1000)
for example
Just keep in mind, that you can await
only inside of async
methodsim confused
Or
Thread.Sleep(1000);
, but this is slightly worse than what ZZZ suggested
Notice how the
void
had to change into async Task
so after if ([var] == 60) how do i make the next few things ONLY happen if thats true and stop happening the moment it stops becoming true (inside of a while (true)
since currently it keeps on going
Break out of the loop
is supposed to do
[var] -= 60
when i reaches 60
wdym?Or use a flag boolean instead of hardcoding
true
in the loop
Literally break;
no that would stop everything entirely i want it to loop every time it reaches 60 to go back to 0
Angius#1586
REPL Result: Success
Console Output
Compile: 535.929ms | Execution: 77.994ms | React with ❌ to remove this embed.
using System;
`
public class Clock
{
public static void Main(string[] args)
{
int second = 0;
int minute = 0;
bool b = Convert.ToBoolean(second);
while (true)
{
second += 1;
Thread.Sleep(1000);
if (second == 60)
{
second -= 0;
minute += 1;
Console.WriteLine(second)
;
}
}
}
}
probably a million and a half issues with it but anyways how do i get it so if
(second == 60)` will make second reset back to 0 and minute go up by oneSet seconds to 0...?
yeah
You're subtracting 0 from seconds now
Instead, set it to 0
oh
a -= b
is a = a - b
well nothing works now
idk why
im confused and tired why did it stop working
using System;
public class Clock
{
public static void Main(string[] args)
{
int second = 0;
int minute = 0;
while (true)
{
second += 1;
Thread.Sleep(1000);
if (second == 60)
{
second -= 60;
minute += 1;
Console.WriteLine(second);
}
}
}
}
Works for me ¯\_(ツ)_/¯
wait how did you
is doing {second} how you display an int as an str
Resets just fine too
String interpolation
well your code is much different
$interpolation
String interpolation is the preferred way of building strings in C#. It is easier to read than concatenation.
For example:
can be written as:
My code, for reference
And it mostly does the same things
a++
is the same as a += 1
do i not need all of the public class and public static void stuff?
Nowadays, no
But you can just put that code inside of the
Main()
and it'll workwdym
???
I mean exactly what I said
wdym putting it inside of main()
using System.Threading;
public class Clock
{
public static void Main()
{
var second = 0;
var minute = 0;
while (true)
{
Thread.Sleep(1000);
second += 1;
if (second == 60)
{
second = 60;
minute += 1;
Console.WriteLine($"{minute}:{second}");
}
}
}
}
second = 60;
why
"If seconds reach 60, set the seconds to 60"?forgot to remove the 6
i wanted to go from -= to = but i forgot to remove the 6 too
Gotcha
wouldnt matter as it doesnt even give me any output in the first palce
That's weird, it should work perfectly fine
Do you have any other code in your project?
nope
That's weird then, because it should work perfectly
if you copy mine does it work
error CS0103: The name 'Console' does not exist in the current context
using System
?
Apparently you're using an older version of .NET that doesn't have implicit usings
Console
is part of the System
namespaceok
The way
using System.Threading
makes Thread
class available, using System
will make Console
availableso i cant have both threading and system?
You can
Just add another
using
Also, I see what's wrong with your code
Since Console.WriteLine()
is inside of the if, not outside like in my code, you'll get output only on full minutesMoving it works just fine
so:
using System;
using Threading;
y
i clicked enter and not shift inter
enter
is that what you mean by using another
using
Yes
slight problem
using System.Threading;
ah ok
at last it works
time to add hours
is there any reason ur coding that class?
?
there are built in classes that handle the time way easier
wdym
i mean u dont need to make this class, C# already has classes that counts the time
well im trying to learn C# and thats the only thing i could think of
ah ok
how do i close
!close
Closed!
that worked
but why warning
its slash close
/close
ah