我正在尝试将加法或减法函数传递给main方法args。在cmd中如果我用1运行程序它应该显示AddFunction,如果我用2按它它应该显示minusFunction。这就是我到目前为止所做的一点点卡住
using System;
namespace addSubtractProject
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Add and Subtract Program!");
Console.WriteLine("============================");
if (args.Length > 0)
{
if (args[0] == "1")
{
Console.WriteLine("You are using the AddFunction");
(AddTwo);
}
else if (args[0] == "2")
{
Console.WriteLine("You are using the subtractFunction");
(subtractTwo);
Console.ReadLine();
}
int x = 10, y = 5;
int z = AddTwo(x, y); // function that returns a value
int i = subtractTwo(x, y);
}
}
//function Add Two Numbers
static int AddTwo(int a, int b)
{
return (a + b);
}
//function Minus Two Numbers
static int subtractTwo(int a, int b)
{
return (a - b);
}
}
}
答案 0 :(得分:1)
几乎没有修改,你可以这样做:
if (args.Length > 0)
{
int x = 10, y = 5;
if (args[0] == "1")
{
Console.WriteLine("You are using the AddFunction");
Console.WriteLine(string.Format("Result = {0}", AddTwo(x,y) ));
}
else if (args[0] == "2")
{
Console.WriteLine("You are using the subtractFunction");
Console.WriteLine(string.Format("Result = {0}", subtractTwo(x,y) ));
}
}
希望这可以给你一些想法。代码可以进一步改进,我会留给你自己尝试。