none
switch语句的用法 RRS feed

  • 问题

  • 说明:

    1、依据大神的推荐,购买的C# 入门经典第7版;亚马逊电子书,正版;

    2、安装vs community 2015,个人免费版,

    问题:

    如书中描述,代码原文如下:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace ConsoleApplication1

    {

    static void Main( string[] args)      

    {        

    const string myName  =  " benjamin";

    const string niceName  =  " andrea";        

    const string sillyName  =  " ploppy";        

    string name;

    WriteLine(" What is your name?");

    name  =   ReadLine();        

    switch (name. ToLower())        

    {          

    case myName:            

    WriteLine(" You have the same name as me!");            

    break;          

    case niceName:            

    WriteLine(" My, what a nice name you have!");            

    break;          

    case sillyName:            

    WriteLine(" That' s a very silly name.");            

    break;        

    }        

    WriteLine($" Hello {name}!");

    ReadKey();      

    }

    }

    }

    []Benjamin Perkins; Jacob Vibe Hammer; Jon D. Reid. C#入门经典(7) C# 6.0 & Visual Studio 2015 (Kindle位置1477). 清华大学出版社. Kindle 版本.

    上述代码调试报错;

    结合之前的代码示例,我修改代码如下:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace ConsoleApplication1

    {

        class Program

        {

            static void Main(string[] args)

            {

                const string myName = " benjamin";

                const string niceName = " andrea";

                const string sillyName = " ploppy";

                string name;

                Console.WriteLine(" What is your name?");

                name =Console.ReadLine();

                switch (name.ToLower())

                {

                    case myName:

                    Console.WriteLine(" You have the same name as me!");

                    break;

                    case niceName:

                        Console.WriteLine(" My, what a nice name you have!");

                    break;

                    case sillyName:

                        Console.WriteLine(" That' s a very silly name.");

                    break;

                }

                Console.WriteLine($" Hello {name}!");

                Console.ReadKey();

            }

        }

    }

    代码调试成功,但是不能按照书中的描述,根据不同的 name 值,在屏幕上显示不同的字符串。

    求答案!

    感谢!

    2017年11月27日 11:39

全部回复

  • 你在控制台输入benjamin或andrea或ploppy,回车后会分别显示

    You have the same name as me!

    My, what a nice name you have!

    That' s a very silly name

    ,并且会输出你输入的字符串。 现在是哪里不符合你的需求?如果输入其它的字符串后回车,则不会有以上的输出。


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2017年11月28日 1:07
  • Hi water.guo,

    欢迎在MSDN论坛发帖。

    代码逻辑没有错误,但是有个地方你写错了, 如下面代码。

               const string myName = " benjamin";
    
                const string niceName = " andrea";
    
                const string sillyName = " ploppy";
    

    你多加了一个空格,这样根据就不能完成你的需求。你可以把空格都给删掉,再测试一下。下面是我测试的截图。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已建议为答案 shao.meng 2017年11月28日 8:21
    2017年11月28日 7:41
    版主
  • Hi,

    感谢你在MSDN论坛发帖。

    如果你的问题解决了,请及时标记有用的回复作为答案,这样会帮助遇到相同问题的人。

     

    Best Regards,

    Har


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年11月29日 6:34
    版主