[英]C# when building a multiple choice quiz, how do we count the correct / wrong answers and form a score base on the counts?
所以我將有 4 個問題,每個問題值 25 分,滿分是 100。我不確定如何根據我的代碼和表格分數代碼來計算答案。 謝謝你幫我。 (我提出了 1 個問題來縮短代碼,但在我的 VS 中有 4 個問題)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MCQuiz
{
class Program
{
static void Main(string[] args)
{
string answer;
Console.WriteLine("Q1-Every Statement in C# language must terminate with: ");
Console.WriteLine(" \t a. ; ");
Console.WriteLine(" \t b. , ");
Console.WriteLine(" \t c. . ");
Console.WriteLine(" \t d. ? ");
Console.WriteLine();
Console.Write("Enter the letter (a, b, c, d) for correct Answer: ")
answer = Console.ReadLine();
if (answer == "a")
{
Console.WriteLine("Your Answer '{0}' is Correct.", answer);
}
else
{
Console.WriteLine("Your Answer '{0}' is Wrong.", answer);
}
Console.WriteLine();
Console.WriteLine("****************************************");
}
}
}
您可以在 integer 變量中跟蹤 totalScore,並使用+=
運算符將每個正確答案加 25,這是以下示例中totalScore = totalScore + 25
的簡寫。
class Program
{
static void Main(string[] args)
{
int totalScore = 0; // initiate variable
string answer;
Console.WriteLine("Q1-Every Statement in C# language must terminate with: ");
Console.WriteLine(" \t a. ; ");
Console.WriteLine(" \t b. , ");
Console.WriteLine(" \t c. . ");
Console.WriteLine(" \t d. ? ");
Console.WriteLine();
Console.Write("Enter the letter (a, b, c, d) for correct Answer: ")
answer = Console.ReadLine();
if (answer == "a")
{
totalScore += 25; // add score
Console.WriteLine("Your Answer '{0}' is Correct.", answer);
}
else
{
Console.WriteLine("Your Answer '{0}' is Wrong.", answer);
}
Console.WriteLine();
Console.WriteLine("****************************************");
Console.WriteLine($"You scored {totalScore} points"); // output result
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.