C
C#17mo ago
daniel1

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

.
53 Replies
daniel1
daniel117mo ago
my attempts so far consits of public static bool (int num = 0)
BlyZe
BlyZe17mo ago
You need to give the method a name public static bool MethodName(int num = 0)
daniel1
daniel117mo ago
i tried public static bool op(int num = 0) its still red
BlyZe
BlyZe17mo ago
You need curly braces after
daniel1
daniel117mo ago
"not all code paths return a vaule
BlyZe
BlyZe17mo ago
ahhh
daniel1
daniel117mo ago
"
BlyZe
BlyZe17mo ago
you need to return a bool for example return true;
daniel1
daniel117mo ago
public static bool op(int num = 0) { if (num > 3) { return "a"; } }
BlyZe
BlyZe17mo ago
thats a string you can just return num > 3;
daniel1
daniel117mo ago
what do you mean by that
BlyZe
BlyZe17mo 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
daniel117mo ago
you sure thats how it should be your not returning anything tho i need to return a massage
BlyZe
BlyZe17mo ago
i dont know what you want to archieve with this method
daniel1
daniel117mo ago
let me send you the task
BlyZe
BlyZe17mo ago
then you have to change the return type
daniel1
daniel117mo ago
one moment
BlyZe
BlyZe17mo ago
Methods - C# Guide
Overview of methods, method parameters, and method return values
daniel1
daniel117mo 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
BlyZe17mo 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
daniel117mo ago
what does char mean
BlyZe
BlyZe17mo ago
char is a character
daniel1
daniel117mo ago
do i need to use char for this
BlyZe
BlyZe17mo ago
you can also use string A char is a single character A string is a char array
Pobiega
Pobiega17mo 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
BlyZe17mo ago
Yeah then char
daniel1
daniel117mo ago
return floors > 3 "a"; this is not working its all red
Pobiega
Pobiega17mo ago
"a" is a string, not a char and also not a valid syntax read BlyZe's code snippet again.
daniel1
daniel117mo ago
it still dosent work why cant i use bool with it
Pobiega
Pobiega17mo ago
what is a bool? what values can a bool represent?
daniel1
daniel117mo ago
i dont know
Pobiega
Pobiega17mo ago
then why do you want to use a bool?
daniel1
daniel117mo ago
but char is not working ethir
Pobiega
Pobiega17mo ago
yes it is, if you write it correctly
MODiX
MODiX17mo 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
Pobiega17mo ago
this will 100% work just fine
daniel1
daniel117mo ago
you sure my teacher will accept this she taught us to use bool and stuff
Pobiega
Pobiega17mo 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
Pobiega17mo ago
Pobiega
Pobiega17mo ago
You don't really seem to be wanting to learn, only cheat on your homework?
daniel1
daniel117mo 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
Pobiega17mo ago
Programming isn't really something you learn in a single evening, just saying.
daniel1
daniel117mo 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
Angius17mo ago
Well, the truth is, that regardless of that you can't learn programming if you only do it during class
daniel1
daniel117mo ago
my 11th grade class is harder compared to other 11th grade classes in my country/ school
Angius
Angius17mo ago
2x45 minutes a week or however much is nothing
daniel1
daniel117mo ago
i know thats why i am sitting here
Angius
Angius17mo 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
daniel117mo 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
Tvde117mo ago
you should follow $helloworld
Tvde1
Tvde117mo ago
I think it's very valuable
Accord
Accord17mo 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.