为什么这个while循环卡住了?

时间:2012-10-20 21:38:00

标签: c# while-loop do-while

我刚写了一个简单的c#代码来计算数字的阶乘,但程序却被卡在姓氏上。可能会有人为什么会被卡住?

谢谢,

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") );
        }
    }
}

} `

3 个答案:

答案 0 :(得分:8)

查看您的while条件:

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

为了突破,所有的子条件都必须为假(因此总值为false)。

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

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

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

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

实际想要:

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

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

答案 1 :(得分: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") );

答案 2 :(得分: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

相关问题