Reputation: 95
i'm doing an exercise in c# to practice but i'm not clearly sure what and how i'm supposed to do things. (i'm not thinking as a compiler yet) :( .
The exercise is set like this.
public bool AreWeInTrouble(bool aSmile, bool bSmile) {}
My code until now:
public bool AreWeInTrouble(bool aSmile, bool bSmile)
{
if (aSmile == true && bSmile == true)
{
return true;
}
else if (aSmile == false && bSmile == false)
{
return true;
}
else
return false;
}
I have put put this method in the main method in Program.cs so once the program starts the method gets called.
static void Main(string[] args)
{
AreWeInTrouble(aSmile, bSmile);
}
But when i call this method i get an error that a and b Smile do not exist in the current content.
So id like to ask if someone can give me some hints/tips for solving this exercise. I'm not sure how to proceed from here but id like to understand what is missing and retry it.
Thank you guys for your help!
Upvotes: 3
Views: 685
Reputation: 1547
This logic can be implemented by simple XNOR logic.
public static bool AreWeInTrouble(bool aSmile, bool bSmile)
{
return aSmile == bSmile;
}
Upvotes: 0
Reputation: 107
What I suggest to you is to create a unit test for this method, so you can put the inputs you want and what is expected from the return. This will make you a better coder for more sophisticated scenarios. link
About the boolean solving, think with me you want something like this:
bool areBothSmilling = aSmile && bSmile; // Booleans don`t need ==, the == will return a boolean;
bool areBothNotSmilling = !aSmile && bSmile == false; // ! and == false will produce the same results
return areBothSmilling || areBothNotSmilling;
But if you think more carefully, you want to return true if the two booleans are equal, so I think this solves it too:
return aSmile == bSmile;
Upvotes: 1
Reputation: 1594
Try this:
static void Main(string[] args) {
Console.WriteLine(AreWeInTrouble(true, true)); // In trouble
Console.WriteLine(AreWeInTrouble(false, false)); // In trouble
Console.WriteLine(AreWeInTrouble(true, false)); // Not in trouble
Console.ReadLine(true);
}
static bool AreWeInTrouble(bool aSmile, bool bSmile) {
// If aSmile matches bSmile then we are in trouble.
return aSmile == bSmile;
}
Upvotes: 4
Reputation: 17631
The AreWeInTrouble
method looks fine (but as a hint: have a look at the exclusive or operator, XOR).
The call in the main method refers to variables, which aren't defined in the scope of the Main
method.
public static void Main()
{
AreWeInTrouble(aSmile, bSmile); // <--- aSmile, bSmile are not defined
}
You can call the method with the boolean constants true
and false
or you'll need to define the variables in the scope of the main method:
public static void Main()
{
bool aSmile = true;
bool bSmile = false;
AreWeInTrouble(aSmile, bSmile); // returns false
AreWeInTrouble(aSmile, true); // returns true
}
And of course, the AreWeInTrouble
method must be declared as static
as the Main
method is static and therefore has no access to instance methods of the class without having an instance.
Upvotes: 1
Reputation: 498
you shoud just declare aSmile and bSmile like this
static void Main(string[] args)
{
bool aSmile=true;
bool bSmile=false;
AreWeInTrouble(aSmile, bSmile);
}
and your method should have this signature
public static bool AreWeInTrouble(bool aSmile,bool bSmile)
Upvotes: 0
Reputation: 156978
You should pass in some values, since aSmile
and bSmile
aren't know variables in that method.
From example:
AreWeInTrouble(true, false);
Second, you should catch the return value:
bool result = AreWeInTrouble(true, false);
And possibly display it:
bool result = AreWeInTrouble(true, false);
Console.WriteLine("Are we in trouble? {0}", result);
Also, make your method static
since the Main
method is:
public static bool AreWeInTrouble(bool aSmile, bool bSmile)
{ }
Upvotes: 8