1

ユーザーが新しい質問を追加できるクイズプロジェクトに取り組んでいます。

質問が保存され、表示のために取得される配列リストがあります。配列リストに保存された各オブジェクトには、5 つの文字列が含まれます。

  • 質問
  • 正しい答え
  • 不正解 1
  • 間違った答え 2
  • 不正解 3

画面に表示するオブジェクトを配列リストからランダムに選択するにはどうすればよいですか? また、毎回異なる位置に正しい答えが表示されるように、4 つの答えを (ラジオ ボタンとして) シャッフルするにはどうすればよいでしょうか?

    namespace quiz
{
    public partial class Quiz : Form
    {
        private ArrayList Questionslist;

        public Quiz(ArrayList list)
        {
            InitializeComponent();
            Questionslist = list;
        }

        int index = 0;
        private void Quiz_Load(object sender, EventArgs e)
        {
            //creating an object of class Question and copying the object at index1 from arraylist into it  
            Question q = (Question)Questionslist[index];
            //to display the contents
            lblQs.Text = q.Quest;
            radioButtonA1.Text = q.RightAnswer;
            radioButtonA2.Text = q.WrongAnswer1;
            radioButtonA3.Text = q.WrongAnswer2;
            radioButtonA4.Text = q.WrongAnswer3;            
        }

        private int Score = 0;

        private void radioButtonA1_CheckedChanged(object sender, EventArgs e)
        {
            //if checkbox is checked
            //displaying text in separate two lines on messagebox
            if (radioButtonA1.Checked == true)
            {
                MessageBox.Show("Well Done" + Environment.NewLine + "You Are Right");
                Score++;
                index++;

                if (index < Questionslist.Count)
                {
                    radioButtonA1.Checked = false;
                    radioButtonA2.Checked = false;
                    radioButtonA3.Checked = false;
                    radioButtonA4.Checked = false;
                    Quiz_Load(sender, e);
                }
                else
                {
                    index--;
                    MessageBox.Show("Quiz Finished" + Environment.NewLine + "your Score is" + Score);
                    Close();
                }
            }
        }

        private void radioButtonA2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonA2.Checked == true)
            {
                MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
                Close();
            }
        }

        private void radioButtonA3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonA3.Checked == true)
            {
                MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
                Close();
            }
        }

        private void radioButtonA4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonA4.Checked == true)
            {
                MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
                Close();
            }
        }
    }
}

これはクラスの質問のコードです

namespace quiz
{
    [Serializable()]
    public class Question : ISerializable
    {
        public String Quest;
        public String RightAnswer;
        public String WrongAnswer1;
        public String WrongAnswer2;
        public String WrongAnswer3;

        public Question()
        {
           Quest = null;
           RightAnswer=null;
           WrongAnswer1=null;
           WrongAnswer2=null;
           WrongAnswer3=null;
        }

        //serialization function
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Question", Quest);
            info.AddValue("Right Answer", RightAnswer);
            info.AddValue("WrongAnswer1",WrongAnswer1);
            info.AddValue("WrongAnswer2",WrongAnswer2);
            info.AddValue("WrongAnswer3",WrongAnswer3);
        }

        //deserializaton constructor
        public Question(SerializationInfo info, StreamingContext context)
        {
            Quest = (String)info.GetValue("Question", typeof(String));
            RightAnswer=(String)info.GetValue("Right Answer",typeof(String));
            WrongAnswer1=(String)info.GetValue("WrongAnswer1",typeof(String));
            WrongAnswer2=(String)info.GetValue("WrongAnswer2",typeof(String));
            WrongAnswer3=(String)info.GetValue("WrongAnswer3",typeof(String));
        }     
    }
}
4

7 に答える 7

1

質問、正しい答え、可能な答えのリストを含むクラスを作成しQuestion、可能な答えをランダムな順序で返し、推測を正しい答えと比較するメソッドを用意します。次のようになります。

class Question
{
    String question;
    String rightAnswer;
    List<String> possibleAnswers = new ArrayList<String>();

    public Question(String question, String answer, List<String> possibleAnswers)
    {
      this.question = question;
      this.rightAnswer = answer;
      this.possibleAnswers.addAll(possibleAnswers);
      this.possibleAnswers.add(this.rightAnswer);
    }

    public List<String> getAnswers()
    {
        Collections.shuffle(possibleAnswers);
        return possibleAnswers;
    }

    public boolean isCorrect(String answer)
    {
      return answer.equals(correctAnswer);
    }
}
于 2013-05-23T14:31:01.627 に答える
1

ArrayList からランダムな文字列を選択します。

string randomPick(ArrayList strings)
{
    return strings[random.Next(strings.Length)];
}

Questionあなたはクラスを持つことを検討するかもしれません。これにはstring Text、質問のメンバーが含まれます (Question.Question はばかげていると思うので、Question.Text を読んだほうが理にかなっています)。あなたが言及したのでArrayList(それが最良の解決策だと思うからではありません)、すべての潜在的な答えを含むメンバーとしてそれらの1つを持つこともできます.

問題作成時に を表示Question.Textし、上記の機能を使ってランダムに回答を選ぶことができます。次に、回答で使用RemoveAt(index)して、回答が重複しないようにします。

于 2013-05-23T14:27:04.163 に答える
1

プロパティに基づいて、どちらが「正しい」答えであるかがわかりTextます。1 つの方法は、回答を配列に格納し、配列をシャッフルしてからラジオ ボタンに割り当てることです。

// You're going to make questionList a List<Question> because if you like
// Right answers you know that ArrayList is Wrong.
Question q = questionList[index];

// You should move this next bit to the Question class
string[] answers = new string[]
    {
        q.RightAnswer,
        q.WrongAnswer1,
        q.WrongAnswer2,
        q.WrongAnswer3
    };

// Using the Fisher-Yates shuffle from:
// http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp
Shuffle(answers);

// Ideally: question.GetShuffledAnswers()

radioButtonA1.Text = answers[0];
radioButtonA2.Text = answers[1];
radioButtonA3.Text = answers[2];
radioButtonA4.Text = answers[3];

その後、必要なのは、すべてが共有する単一のラジオ ボタン イベント ハンドラーだけです。

private void radioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = (RadioButton)sender;
    Question q = questionList[index];

    //if checkbox is checked
    //displaying text in separate two lines on messagebox
    if (rb.Checked && q.RightAnswer == rb.Text)
    {
        // Move your code to another method
        // MessageBox.Show("Well Done" + Environment.NewLine + "You Are Right");
        UserSelectedCorrectAnswer();
    }
    else if (rb.Checked)
    {
        // They checked the radio button, but were wrong!
        // MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
        UserSelectedWrongAnswer();
    }
}
于 2013-05-23T15:09:07.627 に答える
0

シャッフルはおそらく標準的な質問ですが、これはうまくいくと思います:

// Warning: Not a thread-safe type.
// Will be corrupted if application is multi-threaded.
static readonly Random randomNumberGenerator = new Random();


// Returns a new sequence whose elements are
// the elements of 'inputListOrArray' in random order
public static IEnumerable<T> Shuffle<T>(IReadOnlyList<T> inputListOrArray)
{
  return GetPermutation(inputListOrArray.Count).Select(x => inputListOrArray[x]);
}
static IEnumerable<int> GetPermutation(int n)
{
  var list = Enumerable.Range(0, n).ToArray();
  for (int idx = 0; idx < n; ++idx)
  {
    int swapWith = randomNumberGenerator.Next(idx, n);
    yield return list[swapWith];
    list[swapWith] = list[idx];
  }
}

IReadOnlyList<T>(.NET 4.5)をお持ちでない場合は、そのまま使用してIList<T>ください。着信オブジェクトは変更されません。

于 2013-05-23T14:56:57.750 に答える