简体   繁体   中英

trying to create a simple Accessors program

I have 3 classes and one struct and I am trying to print out the total using accessors. The program prints the message the total is . No result is printed. trying to figure this out?

Main:

class MainClass
{
    static void Main(string[] args)
    {
        ArrayList list = new ArrayList();

        list.Add("1. option one");
        list.Add("2. option two");
        list.Add("3. option three");
        list.Add("4. option four");
        list.Add("5. option five");

        Console.WriteLine("1. option one");
        Console.WriteLine("2. option two");
        Console.WriteLine("3. option three");
        Console.WriteLine("4. option four");
        Console.WriteLine("5. option five");
        Console.WriteLine(" Pick  3 options ");

        UserInput usi = new UserInput();
        usi.Get();

        Console.WriteLine(" You Picked ");
        usi.print();

        Console.ReadLine();
    }
}

Program class:

class Program 
{
    private int A, B;
    public void GetAB()
    {
        Console.WriteLine("Enter A: ");
        A = int.Parse(Console.ReadLine());
        if ((A <= 0) || (A >= 6))
          //  Console.WriteLine("good");
            throw new ArgumentOutOfRangeException("Number must be between 1-5");
        Console.WriteLine("Enter B: ");
        B = int.Parse(Console.ReadLine());
        if ((B <= 0) ||(B >=6))
             throw new ArgumentOutOfRangeException("Number must be between 1-5");
    }
    public void PrintAB()
    {
        Console.WriteLine("A = {0}\tb = {1}\t", A, B);
    }
}

user input:

class UserInput : Program
{
    int C;

    public void Get()
    {
        try
        {
            Console.WriteLine("Valid entries are 1-5");
            GetAB();
            Console.Write("Enter C: ");
            C = int.Parse((Console.ReadLine()));
            if ((C <= 0) || (C >= 6))
                throw new ArgumentOutOfRangeException("Number must be between 1-5");
        }
        catch (ArgumentOutOfRangeException ex)
        {
            // Show the user that 7 cannot be divided by 2.
            Console.WriteLine("Error: {0}", ex.Message);
            Console.WriteLine("Please try again ");

            System.Environment.Exit(1);
        }
    }

    public int A
    { get; set; }
    public int B
    { get; set; }

    public void total()
    {
        int total = (A + B + C);
        Console.WriteLine(" Total is ", total);
    }

    public void print()
    {
        PrintAB();
        Console.WriteLine("C = {0}", C);
        total();
    }
}

total:

struct Total
{
    public int total { get; set; }
}

You are doing like:

Console.WriteLine("Total is : ", total);
// In userInput.total()

It should be changed with:

Console.WriteLine("Total is : " + total);

OR

Console.WriteLine("Total is : {0}", total);

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