Switch语句中的C#案例敏感性

时间:2016-11-03 03:53:53

标签: c# .net switch-statement case-sensitive

我正在使用switch语句进行一些工作,并希望了解在输入值时如何忽略区分大小写。

这是我的代码:

using System;

namespace SwitchStatements
{
class MainClass
{
    public static void Main(string[] args)
    {
    Start:
        Console.WriteLine("Please Input the Grade");
        char grade = Convert.ToChar(Console.ReadLine());

        switch (grade)
        {
            case 'A':
                Console.WriteLine("Excellent Work!");
                break;
            case 'B':
                Console.WriteLine("Very Good Effort! Just a couple of     Errors =)");
                break;
            case 'C':
                Console.WriteLine("You Passed. Push Yourself Next Time");
                break;
            case 'D':
                Console.WriteLine("Better put in more effort next time. I know you can do better");
                break;
            default:
                Console.WriteLine("Invalid Grade.");
                break;
        }
        Console.ReadKey();
        goto Start;
    }
}
}

如果我把' a'而不是' A'它返回默认响应。

我可以使用某种类型的比较吗?如果是这样我会把它放在哪里?

8 个答案:

答案 0 :(得分:9)

您可以使用ConsoleKey作为switch的条件,代码如下所示。

var grade =Console.ReadKey().Key;
switch (grade)
{
    case ConsoleKey.A:
        Console.WriteLine("Excellent Work!");
        break;
    case ConsoleKey.B:
        // Something here
        break;    
    case ConsoleKey.C:
        // Something here
        break;

    case ConsoleKey.D:
        // Something here
        break;          
    case ConsoleKey.E:
       // Something here
        break;             
    default:
       // Something here
        break;
}

这样你就可以避免将输入转换为大写/小写,然后再转换为Char。只需在开关内使用ConsoleKey枚举即可。

答案 1 :(得分:6)

您可以使用ToUpper();

<!-- Carousel items -->
<div class="carousel-inner">

    <div class="item active">
        <div class="row">
            <div class="hidden-xs">
                <div class="col-md-6"><a href="https://a2ua.com/picture/picture-002.jpg" target="_blank" class="thumbnail"><img src="https://a2ua.com/picture/picture-002.jpg" class="img-responsive" alt="Image" alt="Image" style="max-width:100%;"></a></div>
                <div class="col-md-6"><a href="https://a2ua.com/picture/picture-002.jpg" target="_blank" class="thumbnail"><img src="https://a2ua.com/picture/picture-002.jpg" class="img-responsive" alt="Image" style="max-width:100%;"></a>
            </div>

            <div class='visible-xs'>
                <div class="col-xs-12">
                    <a href="https://a2ua.com/picture/picture-002.jpg" target="_blank" class="thumbnail"><img src="https://a2ua.com/picture/picture-002.jpg" class="img-responsive" alt="Image" alt="Image" style="max-width:100%;"></a>
                </div>
            </div>

        </div><!--.row-->
    </div><!--.item-->

</div><!--.carousel-inner-->

并且通过Convert.ToChar(Console.ReadLine().ToUpper()); 获取更多字符的错误可以保存,您可以使用

Console.ReadLine()

答案 2 :(得分:2)

您也可以使用以下

   char grade = Convert.ToChar(Console.ReadLine().ToUpperInvariant());  

https://msdn.microsoft.com/en-us/library/system.string.toupperinvariant.aspx

答案 3 :(得分:1)

更改

char grade = Convert.ToChar(Console.ReadLine());

char grade = Convert.ToChar(Console.ReadLine().ToUpper());

https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx

答案 4 :(得分:1)

在切换之前转换为大写,如下所示

grade = Char.ToUpper(grade);

答案 5 :(得分:1)

像这样写开关等级.ToUpper()并且不要改变它的值,可能是你需要它之​​后

    char grade = Convert.ToChar(Console.ReadLine());

    switch (grade.ToUpper())
    {
        case 'A':
            Console.WriteLine("Excellent Work!");
            break;
        case 'B':
            Console.WriteLine("Very Good Effort! Just a couple of     Errors =)");
            break;
        case 'C':
            Console.WriteLine("You Passed. Push Yourself Next Time");
            break;
        case 'D':
            Console.WriteLine("Better put in more effort next time. I know you can do better");
            break;
        default:
            Console.WriteLine("Invalid Grade.");
            break;
    }

答案 6 :(得分:1)

你可能会像这样从一个案件落到另一个案件

public static void Main(string[] args)
{
    Boolean validInputRead = false;
    Char grade;

    while(!validInputRead) 
    {
        validInputRead = true;

        Console.WriteLine("Please Input the Grade");
        grade = Convert.ToChar(Console.Read());

        switch (grade)
        {
            case 'A':
            case 'a':
                Console.WriteLine("Excellent Work!");
                break;

            case 'B':
            case 'b':
                Console.WriteLine("Very Good Effort! Just a couple of     Errors =)");
                break;

            case 'C':
            case 'c':
                Console.WriteLine("You Passed. Push Yourself Next Time");
                break;

            case 'D':
            case 'd':
                Console.WriteLine("Better put in more effort next time. I know you can do better");
                break;

            default:
                Console.WriteLine("Invalid Grade.");
                validInputRead = false;
                break;
        }

        Console.ReadKey();
    }
}

修改

  • 按照建议<{1}}更改为Console.ReadLine()
  • 根据要求添加了Console.Read()

答案 7 :(得分:1)

string letterGrade;             int grade = 0;

        // This will hold the final letter grade
        Console.Write("Input the grade :");

        switch (grade)
        {

            case 1:
                // 90-100 is an A
                letterGrade = "A";
                Console.WriteLine("grade b/n 90-100");
                break;
            case 2:
                // 80-89 is a B
                letterGrade = "B";
                Console.WriteLine("grade b/n 80-89");
                break;
            case 3:
                // 70-79 is a C
                letterGrade = "C";
                Console.WriteLine("grade b/n 70-79");
                break;
            case 4:
                // 60-69 is a D
                letterGrade = "D";
                Console.WriteLine(" grade b/n 60-69 ");
                break;
            default:
                // point whic is less than 59
                Console.WriteLine("Invalid grade");

                break;
        }