【问题标题】:inputs in methods at visual c# 2005Visual c# 2005 方法中的输入
【发布时间】:2011-08-10 06:27:15
【问题描述】:

我在 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)是预定义的,我希望从用户那里获取这两个数字,这意味着用户输入这两个数字,然后同一个程序运行像上面一样。应该怎么做?

P.S 记住一切都应该在方法中完成

【问题讨论】:

  • 下次请格式化您的代码!
  • 除了使用程序参数 (args[]) 提供的答案之外,您还可以使用 Console 和 Console.ReadLine() 方法与用户交互:从以下位置读取下一行字符标准输入流。
  • @David Hall:根据用户界面,您还可以通过文本框等与用户交互:)
  • @Andreas true :) 实际上,我只是注意到 OP 使用了 ReadLine() 他们原来的问题,所以我的评论有点多余。鉴于 OP 似乎具有非常基本的 C# 知识,我试图提供帮助。顺便说一句,非常有帮助的答案。
  • @David:这是讽刺吗? ...听到了...不知何故:)

标签: c# visual-studio arrays methods


【解决方案1】:

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

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,这不是我的主要编码风格...... pfuh......真的很丑!

编辑:

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

【讨论】:

  • 这是谁的编码风格?你的意思是你通常不会用 C# 编写代码?
  • nope :) ...我通常不适应 OP 的风格...但是这里的这个太残酷了...哇...让我想起了我的第一天 :)
  • 哇.. 太残忍了.. 在你给定的代码中,我还没有学到一些东西。请放轻松
  • 残酷是当下的词汇 :) 你的 c# 水平如何?所以我可以调整我的答案...
  • 类程序{ int sum; public int am1(int num1, int num2) { Console.WriteLine("输入第一个值:"); num1 = Int32.Parse(Console.ReadLine()); Console.WriteLine("\n 输入第二个值:"); num2 = Int32.Parse(Console.ReadLine());总和 = 数字 1 + 数字 2;返回总和; } static void Main(string[] args) { 程序 a; a = 新程序(); Console.WriteLine(a.am1(1, 2)); Console.ReadLine(); } }
【解决方案2】:

使用控制台应用程序命令行参数。如果它适合你。下面是一个来自 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.

【讨论】:

    猜你喜欢
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多