我刚写了一个简单的C#代码来计算一个数字的阶乘,但程序被卡在了姓氏上。有人可能为什么它卡住了吗?为什么这个while循环卡住了?
感谢,
10min为单位
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"));
}
}
}
} `
这不找到[factorial](http://en.wikipedia.org/wiki/Factorial)... – ppeterka
与调试器一起代码是你的朋友! –