C
C#14mo ago
RANI

❔ 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
RANI
RANIOP14mo ago
tag me when answer please
SG97
SG9714mo ago
Post your current code, $code
MODiX
MODiX14mo ago
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/
RANI
RANIOP14mo ago
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
Iron
Iron14mo ago
If(number > 0){//code here}
Else {//code here }
If(number > 0){//code here}
Else {//code here }
Tvde1
Tvde114mo ago
Without the capital I and E :)
Iron
Iron14mo ago
Yee mobile autocorrect is annoying 😂
RANI
RANIOP14mo ago
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
Iron
Iron14mo ago
Are you getting the number from user input in console ? Or is the number an int already ? Ahh i see
RANI
RANIOP14mo ago
i get from use r and need program to say if bigger then 0 then true and if lower the 0 then false
Iron
Iron14mo ago
Im on my phone so this could be miss spelled but it gives you a picture to learn :
int.TryParse(Console.ReadLine(),out int x);

If (x >0 ) {//code here } else {//else code here }
int.TryParse(Console.ReadLine(),out int x);

If (x >0 ) {//code here } else {//else code here }
Can also use
int x = Convert.ToInt32(Console.ReadLine)
int x = Convert.ToInt32(Console.ReadLine)
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
public bool myBool;
public bool myBool;
` And in the if statements, you change the bool accordingly like
myBool = true
myBool = true
RANI
RANIOP14mo ago
No description
SinFluxx
SinFluxx14mo ago
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
if (condition)
{
code here;
}
else
{
code here;
}
if (condition)
{
code here;
}
else
{
code here;
}
RANI
RANIOP14mo ago
ye but like let say last line whats problome? like last }
SinFluxx
SinFluxx14mo ago
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
RANI
RANIOP14mo ago
ye it is its pair to the class program
SinFluxx
SinFluxx14mo ago
So you've sorted it?
RANI
RANIOP14mo ago
im trying i dont get what i write in "code here"
SinFluxx
SinFluxx14mo ago
What does your assignment say you need to do?
RANI
RANIOP14mo ago
write a programt that get number if bigget then 0 then true if smaller then program should print flase
SinFluxx
SinFluxx14mo ago
So write a message in the console to the user?
RANI
RANIOP14mo ago
ye user write in console and program should replay in true or false
SG97
SG9714mo ago
how do you write to console in c#?
RANI
RANIOP14mo ago
just run program and console read line no?
SG97
SG9714mo ago
Read? or Write?
RANI
RANIOP14mo ago
read to get user input and then write the give true or false correct me if im wrong
SG97
SG9714mo ago
perfect so use this, and write to console either true or false depending on the if-else
RANI
RANIOP14mo ago
if (x > 0) { console.WriteLine("true"); } else { console.WriteLine("false"); } ?
SG97
SG9714mo ago
c# is case sensitive Console
RANI
RANIOP14mo ago
k k thanks
RANI
RANIOP14mo ago
and like line 26 problom?
No description
RANI
RANIOP14mo ago
its paired with the class { no?
SG97
SG9714mo ago
it is but there's another one
RANI
RANIOP14mo ago
thanks got new problom when i run it i type number in its insta close like console
SinFluxx
SinFluxx14mo ago
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)
RANI
RANIOP14mo ago
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?
SinFluxx
SinFluxx14mo ago
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
RANI
RANIOP14mo ago
ye ye i did the less the 150 and more then 250 if (x > 250) { Console.WriteLine("expensive"); } if (x < 150) { Console.WriteLine("excellent"); }
SinFluxx
SinFluxx14mo ago
ok, so after your if (x > 250) check, you can add another check
else if (condition)
{
}
else if (condition)
{
}
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 250
RANI
RANIOP14mo ago
no way to do like if ( 150 < x < 250)? somthing like that
SinFluxx
SinFluxx14mo ago
You can do something like that yes
RANI
RANIOP14mo ago
if (150 < x < 250) ?
SinFluxx
SinFluxx14mo ago
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 checks
RANI
RANIOP14mo ago
if (x<250 && x>150) { Console.WriteLine("ok); } ? 1 sec
SinFluxx
SinFluxx14mo ago
That is the right syntax, what if entered 250 or 150 though?
RANI
RANIOP14mo ago
i think i got it if (x >= 250) { Console.WriteLine("expensive"); } if (x =< 150) { Console.WriteLine("excellent"); } ?
SinFluxx
SinFluxx14mo ago
blobthumbsup
RANI
RANIOP14mo ago
thanks getting better now thanks for using my program
SinFluxx
SinFluxx14mo ago
So there way I was mentioning you could have done it is:
if(x >= 250)
{
}
else if (x > 150)
{
}
else ()
{
}
if(x >= 250)
{
}
else if (x > 150)
{
}
else ()
{
}
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
Iron
Iron14mo ago
If you want console to stay open after to see what happend use Console.ReadLine() to make it pause and wait for user input
RANI
RANIOP14mo ago
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) ?
SinFluxx
SinFluxx14mo ago
Give it a go 🙂
RANI
RANIOP14mo ago
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(); } } }
SinFluxx
SinFluxx14mo ago
What made you decide to make num1 a Func<string>? You got a number from the console before
RANI
RANIOP14mo ago
wrote num1 show error and that as a suggtion
SinFluxx
SinFluxx14mo ago
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
RANI
RANIOP14mo ago
gimme 1 min to fix it how i can like crate variable for num1 @SinFluxx
SinFluxx
SinFluxx14mo ago
How did you create a variable for 'x' in your previous tasks? As it should be exactly the same as that?
RANI
RANIOP14mo ago
bet i set it up now i do x = Console.ReadLine? @SinFluxx if tags disturbing you lmk imma wont tag
SinFluxx
SinFluxx14mo ago
Remember before you have to use int.Parse or int.TryParse because Console.ReadLine gives you a string not an int
RANI
RANIOP14mo ago
int.TryParse(Console.ReadLine(), out int x); ik how i set console.readLine as x
SG97
SG9714mo ago
when you're done, you might want to study how to properly use int.TryParse
RANI
RANIOP14mo ago
k
SinFluxx
SinFluxx14mo ago
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 variable
RANI
RANIOP14mo ago
i think i got it so like no matter what number i ill write it will be x noting needed to be add ?
SG97
SG9714mo ago
well, kinda
SinFluxx
SinFluxx14mo ago
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
RANI
RANIOP14mo ago
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
SinFluxx
SinFluxx14mo ago
You know how to create a variable?
RANI
RANIOP14mo ago
int y no?
SG97
SG9714mo ago
well, try it
RANI
RANIOP14mo ago
ye ye it is i think no probloms
SinFluxx
SinFluxx14mo ago
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 🙂
RANI
RANIOP14mo ago
k
RANI
RANIOP14mo ago
why it doesnt find way
No description
SinFluxx
SinFluxx14mo ago
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)
RANI
RANIOP14mo ago
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 {}
RANI
RANIOP14mo ago
No description
RANI
RANIOP14mo ago
@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
SG97
SG9714mo ago
classessignto is the first one, and multiply that with classcost?
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server