展开全部
1)新建一个“控制台应用程序"项目,项目名称:SingleChoiceQuestionApp
2)添加一个枚举,枚举名称:ChoiceEnum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SingleChoiceQuestionApp
{
public enum ChoiceEnum
{
None,
A,
B,
C,
D
}
}
3)向项目中添加一个类,类名称:SingleChoiceQuestionApp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SingleChoiceQuestionApp
{
public class SingleChoiceQuestion
{
/// <summary>
/// 题号
/// </summary>
public int QuestionId { get; set; }
/// <summary>
/// 单选题干
/// </summary>
public string Question { get; set; }
/// <summary>
/// 选择项A
/// </summary>
public string ChoiceA { get; set; }
/// <summary>
/// 选择项B
/// </summary>
public string ChoiceB { get; set; }
/// <summary>
/// 选择项C
/// </summary>
public string ChoiceC { get; set; }
/// <summary>
/// 选择项D
/// </summary>
public string ChoiceD { get; set; }
/// <summary>
/// 正确选项
/// </summary>
public ChoiceEnum CorrectChoice { get; set; }
/// <summary>
/// 用户选择的项
/// </summary>
public ChoiceEnum UserChoice { get; set; }
/// <summary>
/// 判断用户选择是否正确
/// </summary>
public bool IsCorrect
{
get { return UserChoice == CorrectChoice; }
}
/// <summary>
/// 显示
/// </summary>
public void ShowQuestion()
{
Console.WriteLine("[{0}].{1}", QuestionId, Question);
Console.WriteLine("A.{0,-30} B.{1,-30}", ChoiceA, ChoiceB);
Console.WriteLine("C.{0,-30} D.{1,-30}", ChoiceC, ChoiceD);
Console.Write("正确的选择是:");
ConsoleKeyInfo c = Console.ReadKey();
switch (c.Key)
{
case ConsoleKey.A :
UserChoice = ChoiceEnum.A;
break;
case ConsoleKey.B:
UserChoice = ChoiceEnum.B;
break;
case ConsoleKey.C:
UserChoice = ChoiceEnum.C;
break;
case ConsoleKey.D:
UserChoice = ChoiceEnum.C;
break;
default:
UserChoice = ChoiceEnum.None;
break;
}
Console.WriteLine();
if (IsCorrect)
{
Console.WriteLine("恭喜你,回答正确!");
}
else
{
Console.WriteLine("回答错误,继续努力!");
}
}
}
}
4)主程序(只给出了3个题目,可以自行修改、扩充)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SingleChoiceQuestionApp
{
class Program
{
static void Main(string[] args)
{
SingleChoiceQuestion t1 = new SingleChoiceQuestion();
t1.QuestionId = 1;
t1.Question = "产自热带的水果是";
t1.ChoiceA = "苹果";
t1.ChoiceB = "栗子";
t1.ChoiceC = "菠萝";
t1.ChoiceD = "鸭梨";
t1.CorrectChoice = ChoiceEnum.C;
t1.ShowQuestion();
Console.WriteLine("-------------");
SingleChoiceQuestion t2 = new SingleChoiceQuestion();
t2.QuestionId = 2;
t2.Question = "企鹅生活在";
t2.ChoiceA = "南极";
t2.ChoiceB = "北极";
t2.ChoiceC = "热带";
t2.ChoiceD = "温带";
t2.CorrectChoice = ChoiceEnum.A;
t2.ShowQuestion();
Console.WriteLine("-------------");
SingleChoiceQuestion t3 = new SingleChoiceQuestion();
t3.QuestionId = 3;
t3.Question = "在导体中可自由移动的是";
t3.ChoiceA = "电子";
t3.ChoiceB = "中子";
t3.ChoiceC = "质子";
t3.ChoiceD = "共价键";
t3.CorrectChoice = ChoiceEnum.A;
t3.ShowQuestion();
Console.WriteLine();
Console.WriteLine("考试结束,按任意键退出");
Console.ReadKey();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询