我正在嘗試編寫一個程序,該程序將獲取用戶輸入值,並詢問他們是否要計算1到n的數字值或n的階乘值! 這是我迄今爲止在C#中計算階乘因子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project_2_Part_B
{
class Program
{
static void Main(string[] args)
{
var Fkeylow = "f";
var FkeyCap = "F";
var Skeylow="s";
var SkeyCap="S";
int n = 0;
long factorial = n;
Console.WriteLine("Input a value n");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Do you want to calculate factorial or sum");
Console.WriteLine("Enter F or S");
var A = Console.ReadLine();
if (A == Fkeylow)
Console.WriteLine();
if (A == FkeyCap)
Console.WriteLine();
var B=Console.ReadLine();
if (B == Skeylow)
Console.WriteLine();
if (B == SkeyCap)
Console.WriteLine();
Console.WriteLine("Press any key to close...");
Console.ReadLine();
}
}
}
我的問題是用計算的語法,以使代碼執行N *(N-1),而N> 1 任何幫助將不勝感激。先進的謝謝!
HTTP://en.wikipedia。 org/wiki/Factorial –
@Stephen你的意思是:input = 10 - > print 10!那麼9!那麼...... 1! ? – 2011-09-03 04:58:26
像用戶輸入5然後按「f」因數輸出應該是120.(5x4x3x2x1) – Thomas