❔ hello i need help writing a code
ok so im learning c# in school and dont remember how to do this we had to do that in class and i got it right but no we need to do it in homework and i kinda forgot
the task if write a programt that get number if bigget then 0 then true if smaller the flase any1 can help me?
81 Replies
tag me when answer please
Post your current code, $code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/like i said i dont remember how to do this
i can write like the
class Program
{
static void Main(string[] args)
{
} } } but is it needed? @sn0wgirl_97
} } } but is it needed? @sn0wgirl_97
Without the capital I and E :)
Yee mobile autocorrect is annoying 😂
sorry for like stupid question i remember bearly nothing
but no need for
int.parse(console.ReadLing()); to get number
if (number > 0) { console.WriteLine("true")};
else { console.WriteLine("false");
im prob wrong but like
Are you getting the number from user input in console ?
Or is the number an int already ?
Ahh i see
i get from use
r
and need program to say if bigger then 0 then true
and if lower the 0 then false
Im on my phone so this could be miss spelled but it gives you a picture to learn :
Can also use
But TryParse uses a try statement so you can catch the exception , for ex if a user types in a string instead of number you would be able to catch that error so i recommend using TryParse
For the true and false just add a bool in the code `
And in the if statements, you change the bool accordingly like
The errors are telling you what's missing, also it will be much easier if you format your code properly, putting the statements on separate lines
ye but like let say last line whats problome?
like last }
Format your code and you might spot it easier
For every opening { in your code there needs to be a closing } so basically you'll need to see if they all pair up
ye it is
its pair to the class program
So you've sorted it?
im trying
i dont get what i write in "code here"
What does your assignment say you need to do?
write a programt that get number if bigget then 0 then true if smaller then program should print flase
So write a message in the console to the user?
ye user write in console and program should replay in true or false
how do you write to console in c#?
just run program
and console read line no?
Read?
or Write?
read to get user input
and then write the give true or false
correct me if im wrong
perfect
so use this, and write to console either
true
or false
depending on the if-else
if (x > 0)
{
console.WriteLine("true");
}
else
{
console.WriteLine("false");
}
?
c# is case sensitive
Console
k k thanks
and like line 26 problom?
its paired with the class { no?
it is
but there's another one
thanks
got new problom when i run it i type number in its insta close
like console
That will be working correctly, it's just because it's done its job and finished running all the code, you could for example add another
Console.ReadLine()
at the end of your method which will make the console stay open to receive more input, or there's a setting in visual studio to make it stay open (I can't remember what the setting is I'm afraid)thanks
now like 7 more question
prob gonna have more question to you guys
thanks for now
yo next 1 is similr
program need to get a number
so like have enter your price
you enter
if lower the 150 print excellent
if betwen 150 and 250 print ok
if above 250 print expensive
and in all case print thanks for using my program after
i know like first and last cuz we did it
how i do like the 150 to 250?
Well, you can write it in a way that checks if it's 150 or more AND less than or equal to 250
Or, if you start with the "expensive" condition you can write it very simply
ye ye i did the less the 150 and more then 250
if (x > 250)
{
Console.WriteLine("expensive");
}
if (x < 150)
{
Console.WriteLine("excellent");
}
ok, so after your
if (x > 250)
check, you can add another check
Do you have an idea what that would be, given that if your code has reach this point you know that the price entered is not bigger than 250no way to do like if ( 150 < x < 250)?
somthing like that
You can do something like that yes
if (150 < x < 250) ?
The format for that is
if (condition1 && condition2)
Note that if you're writing three separate if
statements, and not using else if
it will always run all of the checksif (x<250 && x>150)
{
Console.WriteLine("ok);
}
?
1 sec
That is the right syntax, what if entered 250 or 150 though?
i think i got it
if (x >= 250)
{
Console.WriteLine("expensive");
}
if (x =< 150)
{
Console.WriteLine("excellent");
}
?
thanks
getting better
now thanks for using my program
So there way I was mentioning you could have done it is:
I'm sure you can do that one! You know how to write to the console, and you don't need to check any value before writing it
If you want console to stay open after to see what happend use Console.ReadLine() to make it pause and wait for user input
ye yre
ye ye
thank
s
how i get number and multiple it by let say 48?
num1 = Console.ReadLine
Console.Write.Line (num1 * 48)
?
Give it a go 🙂
1 sec i think i got it
oh nvm 1 sec
nvm
that what i got
{
class Program
{
private static Func<string> num1;
static void Main(string[] args)
{
Console.WriteLine();
num1 = Console.ReadLine;
Console.WriteLine("num1 * 48");
Console.ReadLine();
}
}
}
What made you decide to make num1 a Func<string>?
You got a number from the console before
wrote num1 show error and that as a suggtion
I think if you look back at the last couple of tasks you did you can see how you got a number from the console there
gimme 1 min to fix it
how i can like crate variable
for num1
@SinFluxx
How did you create a variable for 'x' in your previous tasks? As it should be exactly the same as that?
bet
i set it up
now i do x = Console.ReadLine?
@SinFluxx
if tags disturbing you lmk imma wont tag
Remember before you have to use
int.Parse
or int.TryParse
because Console.ReadLine
gives you a string not an intint.TryParse(Console.ReadLine(), out int x);
ik
how i set console.readLine as x
when you're done, you might want to study how to properly use
int.TryParse
k
int.TryParse
is setting the value of x for you, IF it's able to convert the data from Console.ReadLine
to an int
that's what the out int x
part does
which is why really, as said above, you want to check how it works as really you should be checking that it was able to parse the input as an int before using the variablei think i got it
so like no matter what number i ill write it will be x
noting needed to be add
?
well, kinda
Right, it will store the int value to x in this case without you writing any extra code, however if someone entered something in the Console that couldn't be converted to an int then you may run into some issues if you're not checking that the conversion has worked
Obviously it's worked so far because presumably when you've been testing your apps you have only been entering numbers in the console
if i want to setup an other variable
that will be set to like if x > 100 then it will be 43
and if smaller it will be 48?
like
if (x>100)
{
y= 43
}
if (x>0 && x<100)
{
y= 48
}
?
how i set y as a variable to do that
You know how to create a variable?
int y no?
well, try it
ye ye it is
i think
no probloms
Nothing that bad will happen if you try something first and then ask for help when you're truly stuck, you can always undo your changes 🙂
k
why it doesnt find way
Well you've got a problem with the scope of your y variable now
Variables can only be accessed within their scope, for example if I have a method with a variable, and a second method, that second method can't access the variable from the first method by default
The same is true like above where you've declared the y variable (twice) inside the if statements, y now only exists inside of each of those if statements but actually it should exist for all of your Main method
So you need to change where you declare your y variable, and then assign the value 43 or 48 to it in the relevant places
(also you need to think about what happens if the user enters 100 for their number, or a negative number)
ye ye if they enter if 100 its still counts as y=48
and what can i do to block like negative
if x < 0 Console.WriteLine("invalid number") ?
@SinFluxx
so how can i make all of the y to be in the same {}
@SinFluxx
isnt it??
got it
static void Main(string[] args)
{
Console.Write("number of classes");
int classessignto = int.Parse(Console.ReadLine());
int classcost = 100;
int neededforgift = 5;
int totaltopay;
if (classessignto > neededforgift)
{
Console.WriteLine("Gift!!!!");
}
else
{
totaltopay = classessignto * classcost;
}
Console.WriteLine();
Console.WriteLine("{totaltopay}" );
Console.ReadLine();
}
}
}
in here how i make program to print the number of classes sign to
and how much he need to pay
classessignto
is the first one, and multiply that with classcost
?Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.