I am a complete beginner with coding, my first project is to make a simple quiz game. It works like this, I have a textfile with different sentences, when I press a button, the code randomly picks one of the sentences and shows it, its so my quiz game can have multiple questions, so you are able to play it more then once, I found this way to be the best and easiest for me. But! It would also be really good to have a button that shows the answer for the questions right? Thats what I have big trouble with, I have searched and searched for an answer and I simply dont know how to do it or a guide or anything to help me, this is my last resort. the thing is that lets say my textfile with sentences have 5 different sentences it randomly picks from, I want to be make so then I press this button, and lets say the code picks "question3" and shows it, then, I want to have a textfile(or something) that works in the same way, with all the answers, so the first sentence is the answer for question1, second for question2 and so on. I dont know how to code so when I press my randomize button to show one of the questions randomized from my textfile, the right answer should be shown when pressing another button. So if question2 shows, how do I make so my second button shows answer 2? or if it randomizes to question 1, the second button shows the answer for question1 instead? Please if someone out there knows how to help me in a "simple" way, it would mean everything to me. using Windows Forms Application I found this post on the website, seems like it is the same question I need help with, but I cant get that code to work with my code, I have searched a really long time for the answer myself, but I simply cant find anything.. I am really stuck and this is my last resort.. Dont understand the codes I see on different guides and I really need help with this one thing, if anyone out there can help me with this, it would mean the world to me.. This is the code I have:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Quiz_game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label2.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
label2.Visible = true;
Generate();
button1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
label2.Visible = false;
}
private void label2_Click(object sender, EventArgs e)
{
}
void Generate()
{
List<string> Quotes = new List<string>();
Random rand = new Random();
StreamReader quoteReader = new StreamReader("Quotes.txt");
string line = "";
while (!quoteReader.EndOfStream)
{
line = quoteReader.ReadLine();
Quotes.Add(line);
}
label2.Text = Quotes[rand.Next(1, Quotes.Count)];
}
private void button7_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
label2.Visible = true;
Generate();
button3.Visible = false;
}
private void button4_Click(object sender, EventArgs e)
{
label2.Visible = true;
Generate();
button4.Visible = false;
}
private void button5_Click(object sender, EventArgs e)
{
label2.Visible = true;
Generate();
button5.Visible = false;
}
private void button6_Click(object sender, EventArgs e)
{
label2.Visible = true;
Generate();
button6.Visible = false;
}
}
}
Aucun commentaire:
Enregistrer un commentaire