Visual C# 2005 方法中的输入
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望我能满足您的要求...如果没有,请详细说明!
抱歉,这不是我的主要编码风格...pfuh...真的很难看!
编辑:
int.Parse()
。参数来自用户,您最好仔细检查它们!unchecked
- 如果在 vb 中编译,此代码可能会失败并出现OverflowException
- 记住范围int
i hope i got your requirements ... if not, please elaborate!
sry, this is not my main coding style ... pfuh ... really ugly!
edit:
int.Parse()
. the params are coming from the user, you better double check them!unchecked
- this code may fail with anOverflowException
if compiled in vb - remember ranges ofint
使用控制台应用程序命令行参数。如果它适合你。下面是来自 MSDN 的示例。
Use console application command line arguments. If it suites you. Below is an example from MSDN.