简体   繁体   中英

C# Case Sensitivity in Switch-statement

I'm working a little with switch statements and want to know how to ignore the case sensitivity when it comes to input values.

Here is my code:

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

If I put 'a' in instead of 'A' it returns the default response.

Can I use perhaps a .Comparison of some sort? If so where would I put it?

You can use ConsoleKey as condition for switch , the code will be like the following.

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

So that you can avoid converting the input to uppercase/Lower case, and then it goes for another conversion To Char. Simply use ConsoleKey Enumeration inside the switch.

You can use ToUpper(); Like

Convert.ToChar(Console.ReadLine().ToUpper());

and to get saved from the error of getting more charaters with Console.ReadLine() you can use

char grd = Convert.ToChar(Console.ReadKey().KeyChar.ToString().ToUpper());

you can use like following also

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

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

Change

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

To

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

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

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

grade = Char.ToUpper(grade);

Write Switch on grade.ToUpper() like this and don't change change it's value, may be you will need it after

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

You may fall from one case to another like this

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

EDIT

  • Changed from Console.ReadLine() to Console.Read() as suggested
  • Added while(!validInputRead) as requested

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM