计算单词重复的次数

时间:2014-08-13 16:06:02

标签: c# console

我是编程的新手,我正在尝试计算程序中“正确”一词出现的次数,然后在Console.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;

1 个答案:

答案 0 :(得分:0)

您不需要阅读您的计划输出的内容,以了解正确的答案计数。只需使用变量。

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用于您的程序流程,因为它可能会导致多处麻烦。