【问题标题】:How can I [ go to ] using switch我怎样才能[转到]使用开关
【发布时间】:2015-03-12 18:47:02
【问题描述】:

如果用户输入了错误的字符,我该如何编辑此代码?像“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 );

【问题讨论】:

  • 使用while 循环来验证用户输入...?
  • @tnw 对不起,但是我如何使用'while'来验证用户输入?
  • 我们不会做你的作业,每天都会问这个问题。尝试使用搜索。
  • @AhmedIbrahim 请在 Google 上搜索类似 java while loop input validation 的内容。
  • @AhmedIbrahim 然后 google c# while loop input validation 或只是 while loop input validation - 答案将适用于任何 C 系列语言。

标签: c# sharpziplib


【解决方案1】:

您可以使这更加动态和易于阅读。另外,使用字典可以更轻松地添加另一个等级,而无需修改太多代码。

但是,您可以通过跟踪您接受的每个等级的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);

【讨论】:

  • 我几乎看不懂这样的代码! ,我在c#中还是很初学者! :(
  • 我也发布了您的代码,并进行了修改,哈哈。但是,看到使用大量重复来完成任务的痛苦代码很痛苦,这就是为什么我为了自己的理智而重写
  • 第二个代码块应该做你想做的事,尽管@AhmedIbrahim
  • wernl:是的,它工作得很好,但试图理解它,修改后:D 你能帮我,告诉我var 是什么意思? :D 是 int 还是 string 还是 char ?任何类型的日期类型?
  • var 只是一个隐式类型。如果需要,您也可以使用 int,在这种情况下使用它们时,它们在功能上是等效的。您可以在msdn.microsoft.com/en-us/library/bb383973.aspx 阅读有关 var 的更多信息
【解决方案2】:

在之前回答之前没有阅读问题。道歉。检查这个

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

}

【讨论】:

  • 同样的问题:(
猜你喜欢
  • 2022-12-08
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 2021-11-25
  • 1970-01-01
相关资源
最近更新 更多