Я пытаюсь получить факториал, которое будет отображаться как, например, (факториал-равно 5 * 4 * 3 * 2 * 1)C# консоли Выход Форматирование
Я использую метод факториала, но он не принимает строку Console.Write(i + " x ");
в моем коде.
Любая помощь будет замечательной. вот мой код.
//this method asks the user to enter a number and returns the factorial of that number
static double Factorial()
{
string number_str;
double factorial = 1;
Console.WriteLine("Please enter number");
number_str = Console.ReadLine();
int num = Convert.ToInt32(number_str);
// If statement is used so when the user inputs 0, INVALID is outputed
if (num <= 0)
{
Console.WriteLine("You have entered an invalid option");
Console.WriteLine("Please enter a number");
number_str = Console.ReadLine();
num = Convert.ToInt32(number_str);
//Console.Clear();
//topmenu();
//number_str = Console.ReadLine();
}
if (num >= 0)
{
while (num != 0)
{
for (int i = num; i >= 1; i--)
{
factorial = factorial * i;
}
Console.Write(i + " x ");
Console.Clear();
Console.WriteLine("factorial of " + number_str.ToString() + " is " + factorial);
factorial = 1;
Console.WriteLine("(please any key to return to main menu)");
Console.ReadKey();
Console.Clear();
topmenu();
}
}
return factorial;
}
Спасибо!
«i' является« целым числом », вы должны установить его как' string': 'Console.Write (i.ToString() +" x ");' – Tico
'string.Join (" * ", Enumerable .Range (1, 5)) 'is' 1 * 2 * 3 * 4 * 5' –
@TimS. Как это будет * вычислять факториал? –