我刚刚写了一个简单的 c# 代码来计算一个数字的阶乘,但是程序卡在了姓氏上。有人可以为什么它卡住了吗?
谢谢,
欧敏
using System;
//1. Write a program which finds the factorial of a number entered by the user.
namespace Beginner1{
class ProblemOne
{
static void Main (string[] args)
{
bool play = true;
while ( play ) {
Console.Write ("Type in the number you would like to find Factorial of: ");
int num = Convert.ToInt16( Console.ReadLine ());
int sum = 1;
for (int i = 2; i <= num; i++) {
sum = sum * i;
}
Console.WriteLine ("The Factorial of {0} is {1}", num, sum);
Console.Write ( "Would you like to play again? (Y or N): " );
string ans = Console.ReadLine();
do {
if( ans == "Y" || ans == "y" ) {
play = true;
//break;
}
else if( ans == "N" || ans == "n" ) {
play = false;
//break;
}
else
{
Console.Write( "You have entered an invalid answer. Please choose from either Y or N: ");
ans = Console.ReadLine();
}
} while ( (ans != "Y") || (ans != "y") || (ans != "N") || (ans != "n") );
}
}
}
} `