jeudi 10 janvier 2019

How to use global variable for a two condition IF statement in C#

I want user to select from two options which will dictate what a single string says.

Eventually I will construct a sentence with the variables.

It may not make sense much and I'm totally new and doing this for my own enrichment. I know there is probably a lot better ways to construct this but I want to do most of it on my own as I can and have someone look at my finished project and explain what I could do and guide me at that point. First things first though.

I have a working version of this but it doesn't have IF statements with dual conditions. Also I have a class under the project for the construction of the variables and the main class program will generate the output.

class foodReport
{
    public void appleSauce()
    {
        //apple sauce prompt
        Console.WriteLine("Did you have apple sauce:");
        Console.WriteLine("1. Yes");
        Console.WriteLine("2. No");

        //capture key
        var KP = Console.ReadKey();
        Console.Clear();

        //yes no if statement
        if (KP.Key == ConsoleKey.NumPad1)
        {
            int hr = 1;
        }

        if (KP.Key == ConsoleKey.NumPad2)
        {
            int hr = 2;
        }
    }

    public void whatEaten()
    {
        //food prompt
        Console.WriteLine("What did you eat:");
        Console.WriteLine("1. Sandwich");
        Console.WriteLine("2. Candy");

        //capture key
        var KP = Console.ReadKey();
        Console.Clear();

        //selection if statement
        if (KP.Key == ConsoleKey.NumPad1)
        {
            string food = "A sandwich.";
        }

        if (KP.Key == ConsoleKey.NumPad2)
        {
            string food = "Some candy.";
        }
    }

    public void outPut()
    {
        //WHERE IM HAVING TROUBLE
        Console.WriteLine("Desert:");
        Console.WriteLine("1. Cookie");
        Console.WriteLine("2. Pie");

        //capture key
        var KP = Console.ReadKey();
        Console.Clear();

        //selection if statement
        if (KP.Key == ConsoleKey.NumPad1 && hr = 1)
        {
            string report = "You had apple sauce. " + food + " Also, a cookie'";
        }
        if (KP.Key == ConsoleKey.NumPad2)
        {
            string report = "You did not have apple sauce. " + food + " Also, a pie'";   
        }
    }

The if (KP.Key == ConsoleKey.NumPad1 && hr = 1) throws error

Operator && cannot be applied to operands of type bool and int

Aucun commentaire:

Enregistrer un commentaire