计算一个单词重复出现的次数

本文关键字:单词重 一个 计算 | 更新日期: 2023-09-27 18:17:46

我是编程新手,我正试图计算"正确"一词出现在我的程序中的次数,然后在控制台显示该数字。Readline("…");格式。如有任何帮助,不胜感激

Console.WriteLine ("Q1: Solve: 12 x 3");
        Start1:
        int ans1 = Convert.ToInt32 (Console.ReadLine ());
        if (ans1 == 36) {
            Console.WriteLine ("Correct.");
        } else if (ans1 != 36) {
            Console.WriteLine ("Incorrect. Try again.");
        }
        if (ans1 != 36) goto Start1;
        Console.WriteLine ();
        Console.WriteLine ("Q2: 18 ÷ 3");
        Start2:
        int ans2 = Convert.ToInt32 (Console.ReadLine ());
        if (ans2 == 6) {
            Console.WriteLine ("Correct");
        } else if (ans2 != 6) {
            Console.WriteLine ("Incorrect. Try again");
        }
        if (ans2 != 6) goto Start2;
        Console.WriteLine ();
        Console.WriteLine ("Q3: 13 + 34");
        Start3:
        int ans3 = Convert.ToInt32 (Console.ReadLine ());
        if (ans3 == 47) {
            Console.WriteLine ("Correct");
        } else if (ans3 != 47) {
            Console.WriteLine ("Incorrect. Try again");
        }
        if (ans3 != 47) goto Start3;
        Console.WriteLine ();
        Console.WriteLine ("Q4: 6 x 6 x 6");
        Start4:
        int ans4 = Convert.ToInt32 (Console.ReadLine ());
        if (ans4 == 216) {
            Console.WriteLine ("Correct");
        } else if (ans2 != 216) {
            Console.WriteLine ("Incorrect. Try again");
        }
        if (ans4 != 216) goto Start4;
        Console.WriteLine ();
        Console.WriteLine ("Q5: 32 ÷ 4 x 3");
        Start5:
        int ans5 = Convert.ToInt32 (Console.ReadLine ());
        if (ans5 == 24) {
            Console.WriteLine ("Correct");
            Console.WriteLine ();
            Console.WriteLine ("Well done. You have answered all questions correctly with {0} retries.");
        } else if (ans5 != 24) {
            Console.WriteLine ("Incorrect. Try again");
        }
        if (ans5 != 24) goto Start5;

计算一个单词重复出现的次数

您不需要阅读程序输出的内容来知道正确的答案计数。

int correctCount = 0;
int ans1 = Convert.ToInt32 (Console.ReadLine ());
if (ans1 == 36) {
    ++correctCount;
    Console.WriteLine ("Correct.");
}
else if (ans1 != 36) {
    Console.WriteLine ("Incorrect. Try again.");
}
...

这样你就可以知道有多少答案是正确的,而不依赖于输出。

顺便说一句:我强烈建议你不要在你的程序流中使用goto,因为它会导致很多麻烦。