简体   繁体   中英

How can I set up three methods in C# that takes a user input and does math on the input?

This is a beginner's assignment, so I understand people not wanting to just give me the answer. But if possible, I'd appreciate being pointed in the right direction, as I'm very stuck on this prompt:

In a console app project, in a class file, I need to create an object called "Operator." It has to have three methods linked to it, one each for addition, subtraction and division. The program file must ask the user to type an integer, which is then passed through each method and then returned as a result to the console. They didn't specify what to add/subtract/divide by, so I'm going to use Method 1 to add "4" to user input, Method 2 to subtract "3",and last method to divide by "1".

The object is supposed to have its own cs file, and the methods are supposed to be created and called within the program file. I know there's a way to put both class and method in the same file, but for this assignment they want them separate.

I assumed the operator object shouldn't have any properties, since it doesn't have characteristics that I can think of; it's just an abstract object that does things with numbers. So I set up a really simple operator object with nothing in it but the class name:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace threemethods
{
    public class Operator
    {

    }
}

I realize this is likely wrong so far, but I don't know what else needs to be in the class file since the methods will all be in the program file. Now, where I'm really struggling is the program. I don't know where to put everything because none of examples I've seen involve user input, and most are not done externally. I know Console.WriteLine("Pick a number") will probably go in the main method, and addition/subtraction/division will be separate methods below the main one. But other than this, I have no idea the template that this should follow. I'm also unsure how to "pass" the result from one method to the next until all methods have been used. Again, I'm not expecting it to be done for me, but I really need guidance on how to approach this in order to get me going. Even a general outline of where things should go would really help. My program code is so much of a mess right now that I can't share it, as it makes no sense.

Thank you so much!

To Amir:

Thank you so much for your help! Could you explain more on how to format the Console code you've written in order to call the methods? The reason I'm confused is that we are taught to use this format for the program.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace threemethods
{
    class Program
    {
     static void Main(int[] args)

And then the main method underneath that. So, I'm not sure how to correctly integrate your console code when the format the school gives me is a little different. If I place your console code within a program file, can you tell me what code I should add to yours in order to make the program work? For example, what do I write at the top of the file before the Console code?

Thank you so much!! Sorry if what I said is confusing!

Update:

This is cs file about Console:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        public int Data { get; set; }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter input:");
            string line = Console.ReadLine();

            var operatorObject = new Operator(); //You have to add reference, If is not.
            var result = operatorObject.GetAdd(data);

            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

Now in this Operator.CS file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace threemethods
{
    public class Operator
    {
        public double GetAdd(int data)
        {
             data = data + 4;
             return GetSubtract(data);
        }    

        private  double GetSubtract(double data)
        {
             data = data - 3;
             return GetDivide(data);
        }

        private  double GetDivide(double data)
        {
             return data / 3;
        }
    }
}

For do this issue, you have to set private 2 last method, and work with one of them like my sample.

With this sample, you can call just one of the methods and develop so easy.

If you want I can create more sample for you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace threemethods
{
 public class Operator
 {
    public int Add(int data, int value)
    {
         return data + value;
    }    

    public int Subtract(int data, int value)
    {
        return data - value;
    }

    public int Divide(int data, int value)
    {
         return data / value;
    }
 }
}

Note that the all methods are returning an integer. Change it to some floating value if needed.

The main function:

Console.WriteLine("Enter input:");
var userValue = Convert.ToInt32(Console.ReadLine());

var operatorObject = new Operator();
var result = operatorObject.Add(userValue, 4);
result = operatorObject.Subtract(result, 3);
result = operatorObject.Divide(result, 1);

Console.WriteLine(result);
Console.ReadLine();

The other answers have already answered the question, although you seem to be asking a new question about how to implement the answered code? You already hit the nail on the head on the edit to your question, the answer from either above that you prefer (Amir's or Philipp's) is simply written in the main class you've already made! Using Amir's as an example:

using System;

namespace threemethods
{

    public class Operator
    {
        public double GetAdd(int data)
        {
            data = data + 4;
            return GetSubtract(data);
        }

        private double GetSubtract(double data)
        {
            data = data - 3;
            return GetDivide(data);
        }

        private double GetDivide(double data)
        {
            return data / 3;
        }
    }
    class Program
    {
        public static int Data { get; set; }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter input:");
            string line = Console.ReadLine();
            Data = Int32.Parse(line);
            var operatorObject = new Operator();
            var result = operatorObject.GetAdd(Data);
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

Note, you would've thrown an error on main as the arguments for a console entry point are Main(string[] args) and not Main(int[] args). VS doesn't like it if you change this! Also, I recommend not bloating your imports with stuff you don't need. Only using System, only import System. Just because it's default doesn't mean it's needed. Further, using the Data field setup is interesting and not how I would've done it, you probably don't need it? Just using a variable in Main is easier I would think, and more readable with less code. Also, don't forget to parse the string object and convert to an int for operations!!!

This would be your example implementation! Have fun.

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