简体   繁体   中英

Simple C# Calculator with Properties

I understand that this Calculator can be written a lot more simpler than what I have written it but I want to understand the reason it's not working with Properties the way I've written it. I read in a book that I could omit the "set" accessor but I got more errors when I did. I'm getting the Main method error as well. Any help would be appreciated. Line 1(CS5001), 9(CS5113), and 26(CS0161).

using System;

namespace Calculator
{
class Program
{
    public static void Main(string[] args)

    { 

    public int number01;
    public int number02;
    public int Number03

    {
        get

        {
            return number02 / number01;
        }
    }

    class Program1 : Program
    {

        public int DivideFinal()
        {

            Console.Write("Enter a number to be divided: ");
            Console.ReadKey();
            number01 = Convert.ToInt32(Console.ReadKey());
            Console.WriteLine("Enter another number to be divided");
            number02 = Convert.ToInt32(Console.ReadKey());
            Console.WriteLine("The result is: " + Number03);
            Console.ReadKey();
        } 

}

}

}

You have left out two closing braces and claimed a function returns an int which actually returns nothing. It helps to either fix one thing at a time - and in fact try to write only little code at a time, compiling as you go. If you can't see what's wrong comment out large chunks and try to keep your code neatly lined up so you can see where you may have missed a brace or similar.

using System;

namespace Calculator
{
    class Program
    {
        public static void Main(string[] args)
        {

        }//<-----------

        public int number01;
        public int number02;
        public int Number03
        {
            get
            {
                return number02 / number01;
            }
        }//<----------

        class Program1 : Program
        {

            public void DivideFinal()//<---- void not int
            {
                Console.Write("Enter a number to be divided: ");
                Console.ReadKey();
                number01 = Convert.ToInt32(Console.ReadKey());
                Console.WriteLine("Enter another number to be divided");
                number02 = Convert.ToInt32(Console.ReadKey());
                Console.WriteLine("The result is: " + Number03);
                Console.ReadKey();
            }
        }

    }
}

Right - now we have it compiling let's look at the essence of what you have.

namespace Calculator
{
    class Program
    {
        public static void Main(string[] args)
        {
        }
    }
}

You have a class called Program in a namespace with the expected static void Main entry point. It doesn't do anything, so when you run it nothing much will happen. If you run it in a debugger, you might get "Press any key to continue" printed.

If you want something to happen, it needs code in your entry point- this Main function by default.

You have added some properties to this class, which you don't use. You have started to write another class, called Program1 inside this class, which also inherits from this class.

Consider calling it Calculator instead, since that's what you are going to write. It doesn't need to inherit from your main class - they are unrelated. It is much neater to just make a new class in a new file.

Start a new class, and add the properties and other methods like DivideFinal in there. (By the way why have you called it DivideFinal ?)

namespace Calculator
{
    class Calculator
    {
        public int number01;
        public int number02;
        public int Number03
        {
            get
            {
                return number02 / number01;
            }
        }

        public void DivideFinal()
        {
            Console.Write("Enter a number to be divided: ");
            Console.ReadKey();
            number01 = Convert.ToInt32(Console.ReadKey());
            Console.WriteLine("Enter another number to be divided");
            number02 = Convert.ToInt32(Console.ReadKey());
            Console.WriteLine("The result is: " + Number03);
            Console.ReadKey();
        }
    }
}

Finally, let's make the main function do something:

    public static void Main(string[] args)
    {
        var calculator = new Calculator();
        calculator.DivideFinal();
    }

This will lead to some errors, which we can deal with in another question. For example, Convert.ToInt32 expects a String but you are giving it the result of Console.ReadKey which isn't a string. You may wish to consider Console.ReadLine() instead: see this question for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM