請原諒我對這種語言的菜鳥,我是一個初學者。我的任務是創建一個測驗制作工具,但我一直堅持應該如何將我的一些類元素放入串列中。我需要將這些放入串列的原因是因為我不想單獨處理每個用戶輸入的答案,而是在串列中,并且所有內容都取決于串列大小并設定我想要存盤的答案數量限制它。任何幫助將不勝感激。
這是我的課:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz_Maker
{
public class QuestionAndAnswers
{
public string userQuestion { get; set; }
List<string> QnAList = new List<string>();
public string falseAnswerOne { get; set; } //TODO: this could maybe perhaps possilby be a list of string
public string falseAnswerTwo { get; set; } //TODO: this could maybe perhaps possilby be a list of string
public string falseAnswerThree { get; set; } //TODO: this could maybe perhaps possilby be a list of string
public string correctAnswer { get; set; } //TODO: this could maybe perhaps possilby be a list of string
private int correctAnswerIndex;
}
}
這是我的物件方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz_Maker
{
public static class UserInterface
{
public static QuestionAndAnswers GetQuestionAndAnswers()
{
QuestionAndAnswers UserQnA = new QuestionAndAnswers();
string userQuestion;
string correctAnswer;
string falseAnswerOne;
string falseAnswerTwo;
string falseAnswerThree;
Console.WriteLine("Please type your question: ");
UserQnA.userQuestion = Console.ReadLine();
Console.WriteLine("Please type the correct answer: ");
UserQnA.correctAnswer = Console.ReadLine();
Console.WriteLine("Please type your first false answer: ");
UserQnA.falseAnswerOne = Console.ReadLine();
Console.WriteLine("Please type your second false answer: ");
UserQnA.falseAnswerTwo = Console.ReadLine();
Console.WriteLine("Please type your third false answer: ");
UserQnA.falseAnswerThree = Console.ReadLine();
return UserQnA;
}
}
}
我已經用谷歌搜索了這個話題,但沒有找到任何有意義的東西。
uj5u.com熱心網友回復:
我可能會建立一個父子關系。也許是這樣的。
public class QuestionAndAnswers{
public string QuestionText { get; set; }
public IEnumerable<Answer> Answers { get; set; }
}
public class Answer{
public string AnswerText {get; set;}
public bool CorrectAnswer {get; set;}
}
然后你可以填充你的問題和答案串列
List<QuestionAndAnswers> questionAndAnswers = new List<QuestionAndAnswers>
{
new QuestionAndAnswers{
QuestionText = "What color is the sky",
Answers = new List<Answer>{
new Answer{ AnswerText = "Green", CorrectAnswer = false},
new Answer{ AnswerText = "Yellow", CorrectAnswer = false},
new Answer{ AnswerText = "Purple", CorrectAnswer = false},
new Answer{ AnswerText = "Blue", CorrectAnswer = true}
}
},
new QuestionAndAnswers{
QuestionText = "What color is the grass",
Answers = new List<Answer>{
new Answer{ AnswerText = "Green", CorrectAnswer = true},
new Answer{ AnswerText = "Yellow", CorrectAnswer = false},
new Answer{ AnswerText = "Purple", CorrectAnswer = false},
new Answer{ AnswerText = "Blue", CorrectAnswer = false}
}
}
};
uj5u.com熱心網友回復:
我會用一個錯誤串列和一個正確答案串列來模擬這個類,即類似于
public class QuestionAndAnswers {
public string UserQuestion { get; set; }
public string[] FalseAnswers { get; set; }
public string[] CorrectAnswers { get; set; }
}
這使您可以有任意多個答案,如果您愿意,還可以有多個正確答案。但是您可能需要添加一些驗證以確保至少有一個正確答案。
要發布問題,您將合并串列并打亂它們,以便每次順序都是隨機的。要檢查答案是否正確,您只需檢查字串是否存在于正確串列中。例如:
public string[] GetAnswers()
{
var all = FalseAnswers.Concat(CorrectAnswers).ToArray();
all.Shuffle();
return all;
}
public bool IsCorrect(string answer) => CorrectAnswers.Contains(answer);
然后,您可以制作一種方法來實際提出您的問題,并將它們放在一起:
public class QuestionAndAnswers {
public string UserQuestion { get; set; }
public string[] FalseAnswers { get; set; }
public string[] CorrectAnswers { get; set; }
public string[] GetAnswers()
{
var all = FalseAnswers.Concat(CorrectAnswers).ToArray();
all.Shuffle();
return all;
}
public bool IsCorrect(string answer) => CorrectAnswers.Contains(answer);
public bool Ask()
{
Console.WriteLine(UserQuestion);
var answers = GetAnswers();
for (int i = 0; i < answers.Length; i )
{
Console.WriteLine($"{i}: {answers[i]}");
}
for (int i = 0; i < 3; i )
{
while (!int.TryParse(Console.ReadLine(), out var index) || index < 0 || index >= answers.Length)
{
if (!IsCorrect(answers[index]))
{
Console.WriteLine("Correct!");
return true;
}
else
{
Console.WriteLine("False!");
}
}
}
return false;
}
}
uj5u.com熱心網友回復:
您的問題是您將答案存盤在串列和欄位中。你必須選擇一個或另一個。或者可能不是。經過一番思考,您實際上可能走在正確的道路上。讓我解釋。
我認為這里有意義的是有一個正確答案的字串欄位和一個剩余錯誤答案的串列。這樣你就不需要存盤正確答案的索引,這意味著答案的順序不需要固定。
public class QuestionAndAnswers
{
private readonly string _correctAnswer;
public QuestionAndAnswers(string question, string correctAnswer)
{
Question = question;
CorrectAnswer = correctAnswer;
IncorrectAnswers = new List<string>();
}
public string Question { get; }
public string CorrectAnswer { get; set; }
public List<string> IncorrectAnswers { get; }
}
您通過問題和正確答案(總是需要一起去)定義一個瞬間,然后您可以根據需要添加其他答案。
public static UserInterface
{
// ...
public static QuestionAndAnswers NewQuestionAndAnswers()
{
Console.WriteLine("Please type your question: ");
string userQuestion = Console.ReadLine();
Console.WriteLine("Please type the correct answer: ");
string correctAnswer = Console.ReadLine();
Console.WriteLine("Please type your first false answer: ");
string falseAnswerOne = Console.ReadLine();
Console.WriteLine("Please type your second false answer: ");
string falseAnswerTwo = Console.ReadLine();
Console.WriteLine("Please type your third false answer: ");
string falseAnswerThree = Console.ReadLine();
QuestionAndAnswers userQnA = new QuestionAndAnswers(userQuestion, correctAnswer);
userQnA.IncorrectAnswers.AddRange(new string[] { falseAnswerOne, falseAnswerTwo, falseAnswerThree });
return userQnA;
}
}
為了公平起見,答案的順序將在以后確定。這是通過GetShuffledAnswers()函式完成的。
然后要參加測驗,您列出正確和錯誤答案的串列并隨機播放。然后回傳混洗串列,以及正確答案的索引作為out引數(呼叫函式時分配的引數)。所以將以下函式添加到QuestionAndAnswers.
public class QuestionAndAnswers
{
// ...
static readonly Random rng = new Random();
public string[] GetShuffledAnswers(out int correctIndex)
{
var list = IncorrectAnswers.ToList();
list.Add(CorrectAnswer);
list.Sort((x, y) => rng.Next(2) * 2 - 1); // shuffles list
correctIndex = list.IndexOf(CorrectAnswer);
return list.ToArray();
}
}
現在舉一個如何使用上述代碼的例子:
public static UserInterface
{
// ...
public static bool AskQuestion(QuestionAndAnswers userQnA)
{
string[] answers = userQnA.GetShuffledAnswers(out int correctIndex);
Console.WriteLine($"Question: {userQnA.Question}");
Console.WriteLine("Answers:");
for (int i = 0; i < answers.Length; i )
{
Console.WriteLine($" {i 1}. {answers[i]}");
}
int userPick = 0;
do
{
Console.WriteLine("Pick one answer:");
string input = Console.ReadLine();
int.TryParse(input, out userPick);
} while (userPick<=0);
return userPick - 1 == correctIndex;
}
}
請注意,正確的索引 us0..3和用戶用數字選擇他們的答案,1..4這就是檢查的原因userPick - 1 == correctIndex。
帶有一些示例測驗輸出:
Please type your question:
The color of the sky is
Please type the correct answer:
Blue
Please type your first false answer:
Orange
Please type your second false answer:
Black
Please type your third false answer:
Yellow
Question: The color of the sky is
Answers:
1. Orange
2. Blue
3. Black
4. Yellow
Pick one answer:
2
Correct :-)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532198.html
標籤:C#列表班级目的
