切换声明

时间:2009-06-14 01:07:26

标签: c# switch-statement

我正在练习程序中的“Switch Loop”。我正在编写一个用户可以输入整数的代码,在用户输入整数后,它还会显示用户输入的内容。现在我正在尝试实现程序要求用户输入的位置通过选择Y / N再次编号。

我已将它包含在我的代码中但是如果我在第一次输入字符时要求我输入一个整数,程序将执行catch部分。我怎样才能说明如果用户输入一个字符,它也会再次显示该信息,“请输入整数:”

          int enterYourNumber;
          char shortLetter;

            try
            {
                Console.WriteLine("Please enter the integer: ");
                enterYourNumber = Convert.ToInt32(Console.ReadLine());
                WriteNumber(enterYourNumber);

                Console.WriteLine("Do you still want to enter a number? Y/N");
                shortLetter = Convert.ToChar(Console.ReadLine());

                while (shortLetter == 'y' || shortLetter == 'Y')
                {
                    Console.WriteLine("Please enter the integer: ");
                    enterYourNumber = Convert.ToInt32(Console.ReadLine());
                    WriteNumber(enterYourNumber);

                    Console.WriteLine("Do you still want to enter a number? Y/N");
                    shortLetter = Convert.ToChar(Console.ReadLine());
                }

            }
            catch
            {                                        
                Console.WriteLine("Please enter an integer not a character");
            }
        }

            public static void WriteNumber(int wordValue)
            {

            switch (wordValue)
            {
                case 1:
                    Console.WriteLine("You have entered number one");
                    break;
                case 2:
                    Console.WriteLine("You have entered number two");
                    break;
                case 3:
                    Console.WriteLine("You have entered number three");
                    break;
                default:
                    Console.WriteLine("You have exceeded the range of number 1-3 ");
                    break;
            }

这就是我所做的;我不知道为什么我会收到错误。新方法似乎不起作用:

        int enterYourNumber;
        char shortLetter;


          do
        {
            enterYourNumber = GetNumber();
            WriteNumber(enterYourNumber);                
            Console.WriteLine("Do you still want to enter a number? Y/N");
            shortLetter = Convert.ToChar(Console.ReadLine());
         }
       while (shortLetter == 'y' || shortLetter == 'Y')
        {
            Console.WriteLine("Please enter the integer: ");
            enterYourNumber = Convert.ToInt32(Console.ReadLine());
            WriteNumber(enterYourNumber);

            Console.WriteLine("Do you still want to enter a number? Y/N");
            shortLetter = Convert.ToChar(Console.ReadLine());
        }
    }



       public static int GetNumber() 
       {
       bool done = false;
       int value;
       while ( !done ) 
        {
            Console.WriteLine("Please enter the integer: ");
        try 
        {
            value = Convert.ToInt32(Console.ReadLine());
            done = true;
        }
       catch 
        {
            Console.WriteLine("Please enter an integer not a character");
        }
        }
       }

更新

Bill,这是你给出的例子,看来我仍然收到错误:

    public static void Main(string[] args)
    {
        int enterYourNumber;
        char shortLetter;



        do
        {
            enteryourNumber = GetNumber();
            WriteNumber(enteryourNumber);
            Console.WriteLine("Do you still want to enter a number? Y/N");
            shortLetter = Convert.ToChar(Console.ReadLine());
        } 
        while (shortLetter == 'y' || shortLetter == 'Y');
    }


    public static int GetNumber()
    {
        bool done = false;
        int value;
        while (!done)
        {
            Console.WriteLine("Please enter the integer: ");
            try
            {
                value = Convert.ToInt32(Console.ReadLine());
                done = true;
            }
            catch
            {
                Console.WriteLine("Please enter an integer not a character");
            }

            Console.WriteLine("Please enter the integer: ");
            enterYourNumber = Convert.ToInt32(Console.ReadLine());
            WriteNumber(enterYourNumber);
            Console.WriteLine("Do you still want to enter a number? Y/N");
            shortLetter = Convert.ToChar(Console.ReadLine());   
        }
    }

    public static void WriteNumber(int wordValue)
    {

        switch (wordValue)
        {
            case 1:
                Console.WriteLine("You have entered number one");
                break;

            case 2:
                Console.WriteLine("You have entered number two");
                break;

            case 3:
                Console.WriteLine("You have entered number three");
                break;

            default:
                Console.WriteLine("You have exceeded the range of number 1-3 ");
                break;
        }
    }
}
}

更新

此回复是针对罗伯特的。这就是我现在所做的,但是如果我输入“N”,它将不会退出程序。它仍然问同样的问题。

    public static void Main(string[] args)
    {
        int enterYourNumber;
        char shortLetter;
        bool validEntry;



        while (true)
        {
            do
            {
                Console.WriteLine("Please enter an integer: ");
                string numberString = Console.ReadLine();
                validEntry = int.TryParse(numberString, out enterYourNumber);
                WriteNumber(enterYourNumber);
                if (!validEntry)
                {
                    Console.WriteLine("Entry must be an integer");
                }
            } while (!validEntry);

            Console.WriteLine("Do you still want to enter a number? Y/N");
            shortLetter = Convert.ToChar(Console.ReadLine());


        }
     }




    public static void WriteNumber(int wordValue)
    {
        switch (wordValue)
        {
            case 1:
                Console.WriteLine("You have entered number one");
                break;
            case 2:
                Console.WriteLine("You have entered numbered two");
                break;
            case 3:
                Console.WriteLine("You have entered numbered three");
                break;
            default:
                Console.WriteLine("You have exceeded the range of number 1-3");
                break;
        }
    }
}
}

更新

罗伯特和比尔,这就是我现在所拥有的。我怎样才能改善这个?

     public static void Main(string[] args)
     {
        int intEnterYourNumber;
        char charShortLetter;
        string strUserInput;

        do
        {
            do
            {
                Console.WriteLine("Please enter the integer: ");
                strUserInput = Console.ReadLine();
            } while (!int.TryParse(strUserInput, out intEnterYourNumber));
            WriteNumber(intEnterYourNumber);
            Console.WriteLine("Do you still want to enter a number? Y/N");
            charShortLetter = Convert.ToChar(Console.ReadLine().ToUpper());
        } while (charShortLetter == 'Y');
    }



    public static void WriteNumber(int wordValue)
    {
        switch (wordValue)
        {
            case 1:
                Console.WriteLine("You have entered number one");
                break;
            case 2:
                Console.WriteLine("You have entered numbered two");
                break;
            case 3:
                Console.WriteLine("You have entered numbered three");
                break;
            default:
                Console.WriteLine("You have exceeded the range of number 1-3");
                break;
        }
    }

5 个答案:

答案 0 :(得分:8)

如果您没有使用例外来测试您的数字,我认为您的代码会更容易学习和理解。尝试使用 int.TryParse()代替(MSDN上的TryParse())。 TryParse()返回 true (或 false ),具体取决于输入的数字是否有效:

int number;
bool validEntry = int.TryParse(enterYourNumber, out number);
if (!validEntry)
{
    Console.WriteLine("Entry must be an integer.");
}

此外,了解do ... while循环link)。 do..while循环类似于常规while循环,除了在评估条件表达式之前执行一次do-while循环。

bool validEntry;
int enteredNumber;
do
{
    Console.Write("Please enter the integer: ");
    string numberString = Console.ReadLine();
    validEntry = int.TryParse(numberString, out enteredNumber);
    if (!validEntry)
    {
        Console.WriteLine("Entry must be an integer.");
    }
} while (!validEntry);

将整个东西包裹在另一个循环中(“你还想输入一个数字(是/否)吗?”)你已经完成了。

答案 1 :(得分:2)

do
{
    Console.WriteLine("Please enter the integer: ");                
    enterYourNumber = Convert.ToInt32(Console.ReadLine());                
    WriteNumber(enterYourNumber);                
    Console.WriteLine("Do you still want to enter a number? Y/N");                    
    shortLetter = Convert.ToChar(Console.ReadLine());
}                
while (shortLetter == 'y' || shortLetter == 'Y')

答案 2 :(得分:1)

罗伯特是一个开始,但也许我们仍然想尝试抓住...

将主要功能替换为:

do
{
    enterYourNumber = GetNumber();
    WriteNumber(enterYourNumber);                
    Console.WriteLine("Do you still want to enter a number? Y/N");
    shortLetter = Convert.ToChar(Console.ReadLine());
}
while (shortLetter == 'y' || shortLetter == 'Y')

并添加此功能:

public static int GetNumber() {
    boolean done = false;
    int value;
    while ( !done ) {
        Console.WriteLine("Please enter the integer: ");
        try {
            value = Convert.ToInt32(Console.ReadLine());
            done = true;
        }
        catch {
            Console.WriteLine("Please enter an integer not a character");
        }
    }
}

然后在main函数中删除try catch。

答案 3 :(得分:1)

由于您正在学习,您可能还会考虑以不同方式处理错误  请查看SO问题:In C# should try-catch be used for is-numeric testing?以获取更多想法和讨论。

答案 4 :(得分:0)

将try / catch块移动到循环内。罗伯特哈维对do / while循环的建议也很好。

 int enterYourNumber;
 char shortLetter;

 do
 {
      try
      {
          Console.WriteLine("Please enter the integer: ");                
          enterYourNumber = Convert.ToInt32(Console.ReadLine());                
          WriteNumber(enterYourNumber); 
      }
      catch( FormatException )
      {                                        
          Console.WriteLine("Please enter an integer not a character");
      }  

      shortLetter = '\0';
      do
      {
          try
          {
              Console.WriteLine("Do you still want to enter a number? Y/N");                    
              shortLetter = Convert.ToChar(Console.ReadLine());
          }
          catch( FormatException ) 
         {
              Console.WriteLine("Please enter a single character");
         }
      }     
      while ( shortLetter == '\0' )      
 }                
 while (shortLetter == 'y' || shortLetter == 'Y')

 }

        public static void WriteNumber(int wordValue)
        {

        switch (wordValue)
        {
            case 1:
                Console.WriteLine("You have entered number one");
                break;
            case 2:
                Console.WriteLine("You have entered number two");
                break;
            case 3:
                Console.WriteLine("You have entered number three");
                break;
            default:
                Console.WriteLine("You have exceeded the range of number 1-3 ");
                break;
        }