我如何[转]使用开关

时间:2015-03-12 18:47:03

标签: c# sharpziplib

如果用户输入错误的字符,我该如何编辑此代码?喜欢" g"或" H"或其他任何事情,再次重复这一步,不要进入下一步[我的意思]如果我循环10个循环,如果我输入错误的字符,它将循环为9只

 char grade;      // one grade
 int aCount = 0, // number of As
     bCount = 0, // number of Bs
     cCount = 0, // number of Cs
        dCount = 0, // number of Ds
        fCount = 0; // number of Fs

    for ( int i = 1; i <= 10; i++ )
    {
       Console.Write( "Enter a letter grade: " );
       grade = Char.Parse( Console.ReadLine() );
       switch ( grade )
       {
          case 'A': // grade is uppercase A
          case 'a':  // or lowercase a
             ++aCount;
             break;

          case 'B': // grade is uppercase B
          case 'b':  // or lowercase b
             ++bCount;
             break;

          case 'C':  // grade is uppercase C
          case 'c': // or lowercase c
             ++cCount;
             break;

          case 'D': // grade is uppercase D
          case 'd':  // or lowercase d
             ++dCount;
             break;

          case 'F':  // grade is uppercase F
          case 'f': // or lowercase f
             ++fCount;
             break;
          default:    // processes all other characters
             Console.WriteLine( 
                "Incorrect letter grade entered." +
                "\nEnter a new grade" );
             break;
       } // end switch
    } // end for
    Console.WriteLine( 
       "\nTotals for each letter grade are:\nA: {0}" +
       "\nB: {1}\nC: {2}\nD: {3}\nF: {4}", aCount, bCount,
       cCount, dCount, fCount );

2 个答案:

答案 0 :(得分:0)

你可以让它更有活力,更容易阅读。另外,使用字典可以更轻松地添加其他成绩而无需修改如此多的代码。

但是,您可以通过跟踪您接受的每个等级的acceptedGradeCount来解决您的问题,并使用该等级进行10次运行循环。您应该在代码中执行哪些操作。

您还可以使用char.ToLower将字符转换为小写字母,这样您就不需要与字母进行比较。

    //Dictionary of grades with default counts of 0 per grade
    var dict = new Dictionary<char, int>()
    {
        {'a', 0},
        {'b', 0},
        {'c', 0},
        {'d', 0},
        {'f', 0},
    };
    var acceptedGradeCount = 0;
    //While accepted grade count is less than 10
    while (acceptedGradeCount < 10)
    {
        Console.WriteLine("Enter a letter grade: ");
        //Read in the character and convert it to lower case
        var input = char.ToLower(Convert.ToChar(Console.ReadLine()));
        //Determine if the character is a valid grade by seeing if it exists in the dictionary
        if (dict.ContainsKey(input))
        {
            //Add 1 to the dictionary count value for that grade
            dict[input]++;
            acceptedGradeCount++;
        }
        else
        {
            Console.WriteLine("Incorrect letter grade entered. {0}Enter a new grade", Environment.NewLine);
        }
    }

    //Get results string
    var builder = new StringBuilder("Totals for each letter grade are:");
    foreach (KeyValuePair<char, int> keyValuePair in dict)
    {
        builder.Append(string.Format("{0}: {1} ", keyValuePair.Key, keyValuePair.Value));
    }
    //Print Results
    Console.WriteLine(builder.ToString());
    Console.ReadLine();

要继续使用当前代码,您可以添加acceptedGradeCount并在每个已接受的等级中增加它。使用while循环而不是for。

    char grade;      // one grade
    int aCount = 0, // number of As
        bCount = 0, // number of Bs
        cCount = 0, // number of Cs
           dCount = 0, // number of Ds
           fCount = 0; // number of Fs

    var acceptedGradeCount = 0;
    while(acceptedGradeCount < 10)
    {
        Console.Write("Enter a letter grade: ");
        grade = char.ToLower(Char.Parse(Console.ReadLine()));
        switch (grade)
        {
            case 'a':  // or lowercase a
                ++aCount;
                acceptedGradeCount++;
                break;

            case 'b':  // or lowercase b
                ++bCount;
                acceptedGradeCount++;
                break;

            case 'c': // or lowercase c
                ++cCount;
                acceptedGradeCount++;
                break;

            case 'd':  // or lowercase d
                ++dCount;
                acceptedGradeCount++;
                break;

            case 'f': // or lowercase f
                ++fCount;
                acceptedGradeCount++;
                break;
            default:    // processes all other characters
                Console.WriteLine(
                   "Incorrect letter grade entered." +
                   "\nEnter a new grade");
                break;
        } // end switch
    } // end for
    Console.WriteLine(
       "\nTotals for each letter grade are:\nA: {0}" +
       "\nB: {1}\nC: {2}\nD: {3}\nF: {4}", aCount, bCount,
       cCount, dCount, fCount);

答案 1 :(得分:-2)

在回答之前没有读过这个问题。 Appologies。检查一下

static void Main(string[] args)
    {
        char grade;      // one grade
        int aCount = 0, // number of As
            bCount = 0, // number of Bs
            cCount = 0, // number of Cs
               dCount = 0, // number of Ds
               fCount = 0; // number of Fs


        AskForChar(ref aCount, ref bCount, ref cCount, ref dCount, ref fCount); // end switch

    // end for
    Console.WriteLine(
       "\nTotals for each letter grade are:\nA: {0}" +
       "\nB: {1}\nC: {2}\nD: {3}\nF: {4}", aCount, bCount,
       cCount, dCount, fCount);
    Console.ReadLine();
}

private static void AskForChar(ref int aCount, ref int bCount, ref int cCount, ref int dCount, ref int fCount)
{
    for (int i = 1; i <= 10; i++)
    {
        char grade;
        Console.Write("Enter a letter grade: ");
        grade = Char.Parse(Console.ReadLine());
        switch (grade)
        {
            case 'A': // grade is uppercase A
            case 'a':  // or lowercase a
                ++aCount;
                break;

            case 'B': // grade is uppercase B
            case 'b':  // or lowercase b
                ++bCount;
                break;

            case 'C':  // grade is uppercase C
            case 'c': // or lowercase c
                ++cCount;
                break;

            case 'D': // grade is uppercase D
            case 'd':  // or lowercase d
                ++dCount;
                break;

            case 'F':  // grade is uppercase F
            case 'f': // or lowercase f
                ++fCount;
                break;
            default:    // processes all other characters
                Console.WriteLine(
                   "Incorrect letter grade entered." +
                   "\nEnter a new grade");
                return;
                break;
        }
    }

}