Visual C# 2005 方法中的输入

发布于 2024-11-02 20:15:32 字数 536 浏览 0 评论 0原文

我在 Visual Studio c# 2005 中创建控制台应用程序时遇到问题

我创建了以下程序,其中在程序中调用了一个方法(对 2 个预定义值求和),

这是它的代码

class program
{
    static void Main()
    {
        program a;
        a = new program();
        Console.WriteLine(a.am1(1,2));
        Console.ReadLine();
    }
    int sum;
    public int am1(int num1, int num2)
    {
        sum = num1 + num2;
        return sum;
    }
}

现在这是我面临的主要问题面对,在这个程序中预定义了两个整数(num1和num2),我希望从用户那里获取这2个数字,意味着用户输入这两个数字,然后相同的程序像上面一样继续。应该怎么做呢?

PS记住一切都应该在方法中完成

I am facing a problem in creating a console application in Visual Studio c# 2005

I created the following program in which a method (to sum 2 predefined values) is called in the program

here is the code of it

class program
{
    static void Main()
    {
        program a;
        a = new program();
        Console.WriteLine(a.am1(1,2));
        Console.ReadLine();
    }
    int sum;
    public int am1(int num1, int num2)
    {
        sum = num1 + num2;
        return sum;
    }
}

Now here is the main problem I am facing, well in this program two integers (num1 and num2) are predefined, I wanted those 2 numbers to be taken from user, means user input the two numbers and then the same program goes on like above. How it should be done?

P.S remember everything should be done in methods

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2024-11-09 20:15:32

我希望我能满足您的要求...如果没有,请详细说明!

public sealed class Program
{
    private readonly int _number1;
    private readonly int _number2;

    public Program(int number1, int number2)
    {
        this._number1 = number1;
        this._number2 = number2;
    }

    public int Sum()
    {
        return this._number1 + this._number2;
    }

    public static void Main(string[] args)
    {
        // this one here is really brutal, but you can adapt it
        int number1 = int.Parse(args[0]);
        int number2 = int.Parse(args[1]);
        Program program = new Program(number1, number2);
        int sum = program.Sum();
        Console.WriteLine(sum);
        Console.ReadLine();
    }
}

抱歉,这不是我的主要编码风格...pfuh...真的很难看!

编辑:

  1. 不要盲目信任 int.Parse()。参数来自用户,您最好仔细检查它们!
  2. 你最好三重检查它们,因为你正在做一个求和...值得庆幸的是,c# 编译时使用 unchecked - 如果在 vb 中编译,此代码可能会失败并出现 OverflowException - 记住范围int
  3. 为什么要在额外的类中进行简单的加法?
  4. 你应该详细说明你的风格(关于你的评论):将 ui 代码与业务层代码分开!
  5. 您不需要为每个任务创建实例变量 - 您也可以使用范围变量来做到这一点......!
  6. ...

i hope i got your requirements ... if not, please elaborate!

public sealed class Program
{
    private readonly int _number1;
    private readonly int _number2;

    public Program(int number1, int number2)
    {
        this._number1 = number1;
        this._number2 = number2;
    }

    public int Sum()
    {
        return this._number1 + this._number2;
    }

    public static void Main(string[] args)
    {
        // this one here is really brutal, but you can adapt it
        int number1 = int.Parse(args[0]);
        int number2 = int.Parse(args[1]);
        Program program = new Program(number1, number2);
        int sum = program.Sum();
        Console.WriteLine(sum);
        Console.ReadLine();
    }
}

sry, this is not my main coding style ... pfuh ... really ugly!

edit:

  1. don't give blind trust in int.Parse(). the params are coming from the user, you better double check them!
  2. you better triple check them, as you are doing a sum ... thankfully c# compiles with unchecked - this code may fail with an OverflowException if compiled in vb - remember ranges of int
  3. why do you want to do a simple addition in an extra class?
  4. you should elaborate your style (regarding your comment): separate ui-code from business-layer code!
  5. you do not need to create an instance variable for each task - you can do that with scope variables too...!
  6. ...
守护在此方 2024-11-09 20:15:32

使用控制台应用程序命令行参数。如果它适合你。下面是来自 MSDN 的示例。

 public class Functions
    {
        public static long Factorial(int n)
        {
            // Test for invalid input
            if ((n < 0) || (n > 20))
            {
                return -1;
            }

            // Calculate the factorial iteratively rather than recursively:
            long tempResult = 1;
            for (int i = 1; i <= n; i++)
            {
                tempResult *= i;
            }
            return tempResult;
        }
    }

    class MainClass
    {
        static int Main(string[] args)
        {
            // Test if input arguments were supplied:
            if (args.Length == 0)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }

            // Try to convert the input arguments to numbers. This will throw
            // an exception if the argument is not a number.
            // num = int.Parse(args[0]);
            int num;
            bool test = int.TryParse(args[0], out num);
            if (test == false)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }

            // Calculate factorial.
            long result = Functions.Factorial(num);

            // Print result.
            if (result == -1)
                System.Console.WriteLine("Input must be >= 0 and <= 20.");
            else
                System.Console.WriteLine("The Factorial of {0} is {1}.", num, result);

            return 0;
        }
    }
    // If 3 is entered on command line, the
    // output reads: The factorial of 3 is 6.

Use console application command line arguments. If it suites you. Below is an example from MSDN.

 public class Functions
    {
        public static long Factorial(int n)
        {
            // Test for invalid input
            if ((n < 0) || (n > 20))
            {
                return -1;
            }

            // Calculate the factorial iteratively rather than recursively:
            long tempResult = 1;
            for (int i = 1; i <= n; i++)
            {
                tempResult *= i;
            }
            return tempResult;
        }
    }

    class MainClass
    {
        static int Main(string[] args)
        {
            // Test if input arguments were supplied:
            if (args.Length == 0)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }

            // Try to convert the input arguments to numbers. This will throw
            // an exception if the argument is not a number.
            // num = int.Parse(args[0]);
            int num;
            bool test = int.TryParse(args[0], out num);
            if (test == false)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }

            // Calculate factorial.
            long result = Functions.Factorial(num);

            // Print result.
            if (result == -1)
                System.Console.WriteLine("Input must be >= 0 and <= 20.");
            else
                System.Console.WriteLine("The Factorial of {0} is {1}.", num, result);

            return 0;
        }
    }
    // If 3 is entered on command line, the
    // output reads: The factorial of 3 is 6.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文