C
C#2y ago
daniel1

❔ how do i use functions like for example "public static bool (int num = 0)"

.
53 Replies
daniel1
daniel1OP2y ago
my attempts so far consits of public static bool (int num = 0)
BlyZe
BlyZe2y ago
You need to give the method a name public static bool MethodName(int num = 0)
daniel1
daniel1OP2y ago
i tried public static bool op(int num = 0) its still red
BlyZe
BlyZe2y ago
You need curly braces after
daniel1
daniel1OP2y ago
"not all code paths return a vaule
BlyZe
BlyZe2y ago
ahhh
daniel1
daniel1OP2y ago
"
BlyZe
BlyZe2y ago
you need to return a bool for example return true;
daniel1
daniel1OP2y ago
public static bool op(int num = 0) { if (num > 3) { return "a"; } }
BlyZe
BlyZe2y ago
thats a string you can just return num > 3;
daniel1
daniel1OP2y ago
what do you mean by that
BlyZe
BlyZe2y ago
thats true if num is greater then 3 otherwise false
public static bool op(int num = 0)
{
return num > 3;
}
public static bool op(int num = 0)
{
return num > 3;
}
daniel1
daniel1OP2y ago
you sure thats how it should be your not returning anything tho i need to return a massage
BlyZe
BlyZe2y ago
i dont know what you want to archieve with this method
daniel1
daniel1OP2y ago
let me send you the task
BlyZe
BlyZe2y ago
then you have to change the return type
daniel1
daniel1OP2y ago
one moment
BlyZe
BlyZe2y ago
Methods - C# Guide
Overview of methods, method parameters, and method return values
daniel1
daniel1OP2y ago
In a certain city it was determined that a building with more than 3 floors must have an elevator. Write an action that you get a whole number greater than 0, representing the number of floors in the building. The operation will return the character 'a' if required There must be an elevator in the building, otherwise the operation will return the character 'b.'
BlyZe
BlyZe2y ago
Okay so you could do something like this:
public static char NeedsElevator(int floors)
{
return floors > 3 ? 'a' : 'b';
}
public static char NeedsElevator(int floors)
{
return floors > 3 ? 'a' : 'b';
}
daniel1
daniel1OP2y ago
what does char mean
BlyZe
BlyZe2y ago
char is a character
daniel1
daniel1OP2y ago
do i need to use char for this
BlyZe
BlyZe2y ago
you can also use string A char is a single character A string is a char array
Pobiega
Pobiega2y ago
I'd say use char, thats what it seems the task expects.
The operation will return the character 'a' if required
single quotes like that are only used for chars in C#
BlyZe
BlyZe2y ago
Yeah then char
daniel1
daniel1OP2y ago
return floors > 3 "a"; this is not working its all red
Pobiega
Pobiega2y ago
"a" is a string, not a char and also not a valid syntax read BlyZe's code snippet again.
daniel1
daniel1OP2y ago
it still dosent work why cant i use bool with it
Pobiega
Pobiega2y ago
what is a bool? what values can a bool represent?
daniel1
daniel1OP2y ago
i dont know
Pobiega
Pobiega2y ago
then why do you want to use a bool?
daniel1
daniel1OP2y ago
but char is not working ethir
Pobiega
Pobiega2y ago
yes it is, if you write it correctly
MODiX
MODiX2y ago
BlyZe#3482
Okay so you could do something like this:
public static char NeedsElevator(int floors)
{
return floors > 3 ? 'a' : 'b';
}
public static char NeedsElevator(int floors)
{
return floors > 3 ? 'a' : 'b';
}
React with ❌ to remove this embed.
Pobiega
Pobiega2y ago
this will 100% work just fine
daniel1
daniel1OP2y ago
you sure my teacher will accept this she taught us to use bool and stuff
Pobiega
Pobiega2y ago
The task description clearly states that the method should return a char but you do you and absolutely do not copypaste the code Blyze sent it will be more than obvious that you did not write it
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
You don't really seem to be wanting to learn, only cheat on your homework?
daniel1
daniel1OP2y ago
i have an exam tomrow this homework is not graded its just that if you cant to do the home work you probably fail the exam its 10 pm right now
Pobiega
Pobiega2y ago
Programming isn't really something you learn in a single evening, just saying.
daniel1
daniel1OP2y ago
i know i havent done it for three months beacuse our teacher got into a car accidents and we were bombered with exams on every single other subject
Angius
Angius2y ago
Well, the truth is, that regardless of that you can't learn programming if you only do it during class
daniel1
daniel1OP2y ago
my 11th grade class is harder compared to other 11th grade classes in my country/ school
Angius
Angius2y ago
2x45 minutes a week or however much is nothing
daniel1
daniel1OP2y ago
i know thats why i am sitting here
Angius
Angius2y ago
A day before exam Well, it's better than nothing I guess That said, you can easily worsen this code to school levels by using an if-else instead of the ternary
public static string NeedsElevator(int floors)
{
if (floors > 3)
{
return "There is more than three floors";
}
else
{
return "There is no more than three floors";
}
}
public static string NeedsElevator(int floors)
{
if (floors > 3)
{
return "There is more than three floors";
}
else
{
return "There is no more than three floors";
}
}
daniel1
daniel1OP2y ago
i could have studied yestarday but i had an exam yesterday in maths and it does sound like an excuse beacuse it is but i decided that beacuse i handeled that math exam so well i deserved to only study one day before the exam
Tvde1
Tvde12y ago
you should follow $helloworld
Tvde1
Tvde12y ago
I think it's very valuable
Accord
Accord2y 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