First off, analyse the problem. What is going on here? Well the decision about what to print is being made on the relationships between 2,3,7 and a variable. What is the relationship? It is based on whether or not 2,3,7 are factors of the variable, more accurately, it is the pattern of the isFactor status for each of the numbers 2,3,7 with regard to the variable. There are combinations of 3 bool
values that determine what is printed, these can be represented by a truth table e.g. true,true,true false,true false
etc.
There are various ways of coding this. I would use a method that returns a value tuple(bool,bool,bool)
public static (bool,bool,bool)AreTwoThreeSevenFactors(int target)
{
(bool is2, bool is3, bool is7) results;
...
Then I would plug the results into a
switch
statement that returns the answer as a
string
.
private static string ResultsToString((bool, bool, bool) status,int target)
{
return status switch
{
(true, true, true) => "BuzzLightYear",
...
The whole thing can be coded without a single
if
statement. Have a go. Tip : use the discard value for the 'Buzz' option
(true,_,_)