Вывести случайным образом вопросы теста
Цитата: Максим от 12.12.2021, 06:04Здравствуйте, есть задание сделать тест, который выводиться из текстового файла. Сказали что должна быть база в файле примерно из 12 вопросов, и рандомно выводила например 6 вопросов. Я сделал просто вывод из файла, но не могу понять как сделать что бы выводилось только 6 вопросов, и что бы вопросы выводились случайным образом. Прошу помочь.
[spoiler title="Код"]using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Runtime.Remoting.Channels; using System.Text; using System.IO; using System.Threading.Tasks; using System.Windows.Forms; namespace NewKursova { public partial class Form2 : Form { int quection_count; int correct_ans; int wrong_ans; string[] array; int correct_ans_num; int selected_res; System.IO.StreamReader Read; public Form2() { InitializeComponent(); } void start() { var encoding = System.Text.Encoding.GetEncoding(65001); try { Read = new System.IO.StreamReader( System.IO.Directory.GetCurrentDirectory() + @"\test.txt", encoding); //this.Text = Read.ReadLine(); quection_count = 0; correct_ans = 0; wrong_ans = 0; array = new string[10]; } catch (Exception) { MessageBox.Show("Ошибка"); } question(); } void question() { //Random random = new Random(); //File.WriteAllLines("test.txt", File.ReadAllLines("test.txt").OrderBy(s => random.Next()).ToArray()); label1.Text = Read.ReadLine(); radioButton1.Text = Read.ReadLine(); radioButton2.Text = Read.ReadLine(); radioButton3.Text = Read.ReadLine(); correct_ans_num = int.Parse(Read.ReadLine()); radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; //button1.Enabled = false; quection_count = quection_count + 1; if (Read.EndOfStream == true) button1.Text = "Завершити тест"; } void swit(object sender, EventArgs e) { button1.Enabled = true; button1.Focus(); RadioButton переключатель = (RadioButton)sender; var tmp = переключатель.Name; selected_res = int.Parse(tmp.Substring(11)); } private void button1_Click(object sender, EventArgs e) { if (selected_res == correct_ans_num) correct_ans = correct_ans + 1; if (selected_res != correct_ans_num) { wrong_ans = wrong_ans + 1; array[wrong_ans] = label1.Text; } if (button1.Text == "Почати тестування спочатку") { button1.Text = "Наступне питання"; radioButton1.Visible = true; radioButton2.Visible = true; radioButton3.Visible = true; start(); return; } if (button1.Text == "Завершити тест") { Read.Close(); radioButton1.Visible = false; radioButton2.Visible = false; radioButton3.Visible = false; label1.Text = String.Format("Тестування завершено.\n " + "Правильних відповідей: {0} из {1}. \n " + "Набрано балів по п'ятибальній системі: {2:F2}", correct_ans, quection_count, (correct_ans * 5.0f) / quection_count); button1.Text = "Почати тестування спочатку"; var str = "Список ошибок " + ":\n\n"; for (int i = 1; i <= wrong_ans; i++) str = str + array[i] + "\n"; if (wrong_ans != 0) MessageBox.Show(str, "Тестування завершено"); } if (button1.Text == "Наступне питання") question(); } private void label1_Click(object sender, EventArgs e) { } private void radioButton1_CheckedChanged(object sender, EventArgs e) { } private void radioButton2_CheckedChanged(object sender, EventArgs e) { } private void radioButton3_CheckedChanged(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void Form2_Load(object sender, EventArgs e) { timer1.Start(); button1.Text = "Наступне питання"; button2.Text = "Вийти"; radioButton1.CheckedChanged += new EventHandler(swit); radioButton2.CheckedChanged += new EventHandler(swit); radioButton3.CheckedChanged += new EventHandler(swit); start(); } private void radioButton4_CheckedChanged(object sender, EventArgs e) { } int timeleft = 600; private void timer1_Tick(object sender, EventArgs e) { if (timeleft > 0) { timeleft = timeleft - 1; label2.Text = "Залишилось часу " + timeleft + " секунд"; } else { button1.Enabled = false; timer1.Stop(); label2.Text = "Время закончилось"; } } } }[/spoiler]
Пример теста
где 1ряд-label, 2,3,4-варианты ответа, 5-правильный ответ[spoiler title="Пример"]Какой из ниже перечисленных операторов, не является циклом
do while
for
repeat until
3
|Цикл с предусловием
for
do while
while
3
|Язык програмирования разработал
Кен Томпсон
Дональд Кнут
Бьерн Страуструп
3
[/spoiler]
[MNotice]Как оформлять свои темы и комментарии[/MNotice]
Здравствуйте, есть задание сделать тест, который выводиться из текстового файла. Сказали что должна быть база в файле примерно из 12 вопросов, и рандомно выводила например 6 вопросов. Я сделал просто вывод из файла, но не могу понять как сделать что бы выводилось только 6 вопросов, и что бы вопросы выводились случайным образом. Прошу помочь.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NewKursova
{
public partial class Form2 : Form
{
int quection_count;
int correct_ans;
int wrong_ans;
string[] array;
int correct_ans_num;
int selected_res;
System.IO.StreamReader Read;
public Form2()
{
InitializeComponent();
}
void start()
{
var encoding = System.Text.Encoding.GetEncoding(65001);
try
{
Read = new System.IO.StreamReader(
System.IO.Directory.GetCurrentDirectory() +
@"\test.txt", encoding);
//this.Text = Read.ReadLine();
quection_count = 0;
correct_ans = 0;
wrong_ans = 0;
array = new string[10];
}
catch (Exception)
{
MessageBox.Show("Ошибка");
}
question();
}
void question()
{
//Random random = new Random();
//File.WriteAllLines("test.txt", File.ReadAllLines("test.txt").OrderBy(s => random.Next()).ToArray());
label1.Text = Read.ReadLine();
radioButton1.Text = Read.ReadLine();
radioButton2.Text = Read.ReadLine();
radioButton3.Text = Read.ReadLine();
correct_ans_num = int.Parse(Read.ReadLine());
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
//button1.Enabled = false;
quection_count = quection_count + 1;
if (Read.EndOfStream == true) button1.Text = "Завершити тест";
}
void swit(object sender, EventArgs e)
{
button1.Enabled = true;
button1.Focus();
RadioButton переключатель = (RadioButton)sender;
var tmp = переключатель.Name;
selected_res = int.Parse(tmp.Substring(11));
}
private void button1_Click(object sender, EventArgs e)
{
if (selected_res == correct_ans_num) correct_ans = correct_ans + 1;
if (selected_res != correct_ans_num)
{
wrong_ans = wrong_ans + 1;
array[wrong_ans] = label1.Text;
}
if (button1.Text == "Почати тестування спочатку")
{
button1.Text = "Наступне питання";
radioButton1.Visible = true;
radioButton2.Visible = true;
radioButton3.Visible = true;
start(); return;
}
if (button1.Text == "Завершити тест")
{
Read.Close();
radioButton1.Visible = false;
radioButton2.Visible = false;
radioButton3.Visible = false;
label1.Text = String.Format("Тестування завершено.\n " + "Правильних відповідей: {0} из {1}. \n " + "Набрано балів по п'ятибальній системі: {2:F2}", correct_ans, quection_count, (correct_ans * 5.0f) / quection_count);
button1.Text = "Почати тестування спочатку";
var str = "Список ошибок " + ":\n\n";
for (int i = 1; i <= wrong_ans; i++)
str = str + array[i] + "\n";
if (wrong_ans != 0) MessageBox.Show(str, "Тестування завершено");
}
if (button1.Text == "Наступне питання") question();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form2_Load(object sender, EventArgs e)
{
timer1.Start();
button1.Text = "Наступне питання";
button2.Text = "Вийти";
radioButton1.CheckedChanged += new EventHandler(swit);
radioButton2.CheckedChanged += new EventHandler(swit);
radioButton3.CheckedChanged += new EventHandler(swit);
start();
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
}
int timeleft = 600;
private void timer1_Tick(object sender, EventArgs e)
{
if (timeleft > 0)
{
timeleft = timeleft - 1;
label2.Text = "Залишилось часу " + timeleft + " секунд";
}
else
{
button1.Enabled = false;
timer1.Stop();
label2.Text = "Время закончилось";
}
}
}
}
Пример теста
где 1ряд-label, 2,3,4-варианты ответа, 5-правильный ответ
do while
for
repeat until
3
|Цикл с предусловием
for
do while
while
3
|Язык програмирования разработал
Кен Томпсон
Дональд Кнут
Бьерн Страуструп
3
Комментарий модератора:
Как оформлять свои темы и комментарии