我有一个可工作的组合控制台应用程序,但我想做一些更改,我需要一些帮助



你好,我无法在标题中解释它,但我想做的是使r永久"2〃;我不希望它在我打开应用程序时显示给我。所以它应该只问我n";4〃;例如,r总是应该是2。我知道我在问一些复杂的事情,但我试了几个小时,但没有进展,你能帮我吗。

class Program
{
static int combin(int a, int b)
{
int f1, f2, f3, y;
f1 = fact(a);
f2 = fact(b);
f3 = fact(a - b);
y = f1 / (f2 * f3);
return y;
}

static int fact(int x)
{
int fx = 1, i;
for (i = 1; i <= x; i++)
fx = fx * i;
return fx;
}

static void Main(string[] args)
{
int n, r, comb;
Console.WriteLine("n ve r degerlerini giriniz");
n = Convert.ToInt32(Console.ReadLine());
r = Convert.ToInt32(Console.ReadLine());
comb = combin(n, r);
Console.WriteLine("Sonuç:{0}",comb);
Console.ReadLine();

}
}

只是不要要求"r"并将变量设置为"2":

static void Main(string[] args)
{
int n, comb; // don't ask for 'r'
Console.WriteLine("n ve r degerlerini giriniz");
n = Convert.ToInt32(Console.ReadLine());
//r = Convert.ToInt32(Console.ReadLine()); // don't ask for 'r'
int r = 2;
comb = combin(n, r);
Console.WriteLine("Sonuç:{0}",comb);
Console.ReadLine();

}

如果有人将来需要,下面是完整的代码,再次感谢Jonathan。

static int combin(int a, int b)
{
int f1, f2, f3, y;
f1 = fact(a);
f2 = fact(b);
f3 = fact(a - b);
y = f1 / (f2 * f3);
return y;
}
static int fact(int x)
{
int fx = 1, i;
for (i = 1; i <= x; i++)
fx = fx * i;
return fx;
}
static void Main(string[] args)
{
int n, comb;
Console.WriteLine("n ve r degerlerini giriniz: ");
n = Convert.ToInt32(Console.ReadLine());
//r = Convert.ToInt32(Console.ReadLine());
int r = 2;
comb = combin(n, r);
Console.WriteLine("Sonuç:{0}", comb);
Console.ReadLine();
}

最新更新