简体   繁体   English

如何在C#中设置三种方法来接受用户输入并对输入进行数学运算?

[英]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." 在控制台应用程序项目的类文件中,我需要创建一个名为“ 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". 他们没有指定要加/减/除的内容,因此我将使用方法1将“ 4”添加到用户输入中,使用方法2减去“ 3”,最后使用方法除以“ 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. 该对象应该具有自己的cs文件,并且方法应该在程序文件中创建和调用。 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. 我知道Console.WriteLine(“ Pick a number”)可能会进入main方法,而加法/减法/除法将是main方法之下的单独方法。 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: 我感到困惑的原因是,我们被教导要对program.cs文件使用这种格式:

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: 这是关于控制台的cs文件:

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: 现在在此Operator.CS文件中:

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. 为此,您必须设置private 2 last方法,并像我的示例一样使用其中的一个。

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! 您已经在问题的编辑上敲了钉子,上面想要的答案(Amir或Philipp的答案)只是写在已经完成的主类中! Using Amir's as an example: 以Amir's为例:

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). 注意,您会在main上抛出错误,因为控制台入口点的参数是Main(string [] args)而不是Main(int [] args)。 VS doesn't like it if you change this! 如果您更改此设置,VS会不喜欢! 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. 我认为,仅在Main中使用一个变量会更容易,并且用更少的代码就更易读。 Also, don't forget to parse the string object and convert to an int for operations!!! 另外,不要忘记解析字符串对象并将其转换为int进行操作!!!

This would be your example implementation! 这将是您的示例实现! Have fun. 玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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