Home > Mobile >  Creating a magic 8 ball program c#
Creating a magic 8 ball program c#

Time:10-18

I need help with a homework I have in school. These are the requirements from my teacher.

Program requirements:

  • Accept user input of a question.
  • Output a result based upon a real "magic 8-ball" (do not add any of your own)
  • use a switch statement

Create a Method with the following signature:

public static string responses ()

All user input and output should be done in your main method.

This is my code and I have a problem with returning the value and Visual Studio highlights the command break and reports an CS0162 Unreachable code detected.

using System;
public class Magic8Ball
{
    public static void Main(string[] args)
    {

        Console.WriteLine("Ask you question to the Magic 8 Ball: ");
        Console.ReadLine();
        string response;

        response = responses(response);
        Console.WriteLine(response);

    }
    public static string responses(string response)
    {

        string affirmativeResponse1 = "It is certain.";
        string affirmativeResponse2 = "It is decidedly so.";
        string affirmativeResponse3 = "Without a doubt.";
        string affirmativeResponse4 = "Yes definitely.";
        string affirmativeResponse5 = "You may rely on it.";

        string affirmativeResponse6 = "As I see it, yes.";
        string affirmativeResponse7 = "Most likely.";
        string affirmativeResponse8 = "Outlook good.";
        string affirmativeResponse9 = "Yes.";
        string affirmativeResponse10 = "Signs point to yes.";

        string nonCommittalResponse1 = "Reply hazy, try again.";
        string nonCommittalResponse2 = "Ask again later.";
        string nonCommittalResponse3 = "Better not tell you now.";
        string nonCommittalResponse4 = "Cannot predict now.";
        string nonCommittalResponse5 = "Concentrate and ask again.";

        string negativeResponse1 = "Don't count on it.";
        string negativeResponse2 = "My reply is no.";
        string negativeResponse3 = "My sources say no.";
        string negativeResponse4 = "Outlook not so good.";
        string negativeResponse5 = "Very doubtful.";

        int numberOfResponse;
        Random var = new Random();
        numberOfResponse = var.Next(20);

        switch (numberOfResponse)
        {
            case 0:
                response = affirmativeResponse1;
                return response;
                break;
            case 1:
                response = affirmativeResponse2;
                return response;
                break;
            case 2:
                response = affirmativeResponse3;
                return response;
                break;
            case 3:
                response = affirmativeResponse4;
                return response;
                break;
            case 4:
                response = affirmativeResponse5;
                return response;
                break;
            case 5:
                response = affirmativeResponse6;
                return response;
                break;
            case 6:
                response = affirmativeResponse7;
                return response;
                break;
            case 7:
                response = affirmativeResponse8;
                return response;
                break;
            case 8:
                response = affirmativeResponse9;
                return response;
                break;
            case 9:
                response = affirmativeResponse10;
                return response;
                break;
            case 10:
                response = nonCommittalResponse1;
                return response;
                break;
            case 11:
                response = nonCommittalResponse2;
                return response;
                break;
            case 12:
                response = nonCommittalResponse3;
                return response;
                break;
            case 13:
                response = nonCommittalResponse4;
                return response;
                break;
            case 14:
                response = nonCommittalResponse5;
                return response;
                break;
            case 15:
                response = negativeResponse1;
                return response;
                break;
            case 16:
                response = negativeResponse2;
                return response;
                break;
            case 17:
                response = negativeResponse3;
                return response;
                break;
            case 18:
                response = negativeResponse4;
                return response;
                break;
            case 19:
                response = negativeResponse5;
                return response;
                break;


        }
    }
}

CodePudding user response:

When you return a value from a function, nothing after that return can be ran - you're leaving the function. Therefore the breaks will never be reached because there is always a return right before them.

You also don't need to assign the return value to the response variable, you can just write

return affirmativeResponseX;

CodePudding user response:

the "return" keyword pretty much ends the method so you don't really need to add the "break;" in this instance. Also you need to a default condition when using switch statements and lastly your string response needs to be set to the console.readline.

        {
            public static void Main(string[] args)
            {

                Console.WriteLine("Ask you question to the Magic 8 Ball: ");
                
                string response = Console.ReadLine(); 

                response = Responses(response);
                Console.WriteLine(response);

            }
            public static string Responses(string response)
            {

                string affirmativeResponse1 = "It is certain.";
                string affirmativeResponse2 = "It is decidedly so.";
                string affirmativeResponse3 = "Without a doubt.";
                string affirmativeResponse4 = "Yes definitely.";
                string affirmativeResponse5 = "You may rely on it.";

                string affirmativeResponse6 = "As I see it, yes.";
                string affirmativeResponse7 = "Most likely.";
                string affirmativeResponse8 = "Outlook good.";
                string affirmativeResponse9 = "Yes.";
                string affirmativeResponse10 = "Signs point to yes.";

                string nonCommittalResponse1 = "Reply hazy, try again.";
                string nonCommittalResponse2 = "Ask again later.";
                string nonCommittalResponse3 = "Better not tell you now.";
                string nonCommittalResponse4 = "Cannot predict now.";
                string nonCommittalResponse5 = "Concentrate and ask again.";

                string negativeResponse1 = "Don't count on it.";
                string negativeResponse2 = "My reply is no.";
                string negativeResponse3 = "My sources say no.";
                string negativeResponse4 = "Outlook not so good.";
                string negativeResponse5 = "Very doubtful.";

                int numberOfResponse;
                Random var = new Random();
                numberOfResponse = var.Next(20);

                switch (numberOfResponse)
                {
                    case 0:
                        response = affirmativeResponse1;
                        return response;
                    case 1:
                        response = affirmativeResponse2;
                        return response;
                    case 2:
                        response = affirmativeResponse3;
                        return response;
                    case 3:
                        response = affirmativeResponse4;
                        return response;
                    case 4:
                        response = affirmativeResponse5;
                        return response;
                    case 5:
                        response = affirmativeResponse6;
                        return response;
                    case 6:
                        response = affirmativeResponse7;
                        return response;
                    case 7:
                        response = affirmativeResponse8;
                        return response;
                    case 8:
                        response = affirmativeResponse9;
                        return response;
                    case 9:
                        response = affirmativeResponse10;
                        return response;
                    case 10:
                        response = nonCommittalResponse1;
                        return response;
                    case 11:
                        response = nonCommittalResponse2;
                        return response;
                    case 12:
                        response = nonCommittalResponse3;
                        return response;
                    case 13:
                        response = nonCommittalResponse4;
                        return response;
                    case 14:
                        response = nonCommittalResponse5;
                        return response;
                    case 15:
                        response = negativeResponse1;
                        return response;
                    case 16:
                        response = negativeResponse2;
                        return response;
                    case 17:
                        response = negativeResponse3;
                        return response;
                    case 18:
                        response = negativeResponse4;
                        return response;
                    case 19:
                        response = negativeResponse5;
                        return response;
                    default:                           
                        response = affirmativeResponse1;
                        return response;

                }
            }
        }    ```

CodePudding user response:

This is because when you return, you exit the function. It will never reach the break because you return right before it. The fix is simple. Just remove the breaks.

  •  Tags:  
  • c#
  • Related