0

我刚刚写了一个简单的 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") );
        }
    }
}

} `

4

3 回答 3

8

看你的while情况:

while ( (ans != "Y") || (ans != "y") || (ans != "N") || (ans != "n") )

为了突破,所有这些子条件都必须为假(因此整体值为假)。

第一个条件成立的唯一方法是如果ans是“Y”......这意味着它绝对不会等于“y”......

换句话说,您的循环等于:

while (!(ans == "Y" && ans == "y" && ans == "N" && ans == "n"))

它必须是一个非常特殊的字符串才能等于所有四个值。

实际上想要:

while (ans != "Y" && ans != "y" && ans != "N" || ans != "n")

换句话说,在值不等于任何所需值时继续前进。(另一种选择是保留一组“好的答案”......

于 2012-10-20T21:40:28.363 回答
0
        do {
            string ans = Console.ReadLine();
            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") );
于 2012-10-20T21:43:09.300 回答
0

我会做这样的事情:

bool isValid = false;

        do
        {

            if( ans == "Y" || ans == "y" )  {
                play = true;
                isValid = true;
                //break;
            }
            else if( ans == "N" || ans == "n" ) {
                play = false;
                isValid = true;
                //break;
            }
            else
            {
                Console.Write( "You have entered an invalid answer. Please choose from either Y or N: ");
                ans = Console.ReadLine();
                isValid = false;
            }
        }while(isValid == false);

相关小提琴:https ://dotnetfiddle.net/bxHY27

于 2016-07-14T18:08:17.563 回答