Quiz with multiples choice - Beginner level

Hi buddies ! :smiley:

I’m looking about how to make a quiz,
I have started few test with a part of success.
However I’d like to make a quiz with multiples choices.

So I’d like to give him ( the user ) three choices including the right answer.

Here’s my question, how to show him three answers ( on the graphical window or pther way ) while the question is on waiting an answer ?

Hi,

Show us the code that you have so far.

There can be many ways to do what you want and depending on how you wrote your code we will give you different solutions.

It won’t help you !

I tried most of the things on others programs that I removed…

I’m completely lost btw
No idea how to do
<

>

please post your code as COPY PASTE from
processing IDE
into the forum editor

</> code format tag

here a basic example how to build a array of questions ( string )
for a multiline concept
but only use one text() command
possibly you can use.

String[] qs = { 
  "question1:\nwhat is bigger\n-a- 2 \n-b- 5\n-c- 1", 
  "", 
  "" 
};
int point = 0;

void setup() {
  size(300, 300);
  textSize(20);
}

void draw() {
  background(200, 200, 0);
  fill (0, 0, 200);
  text(qs[point], 40, 60);
}


1 Like

That is very interesting ! I appreciate the help !

But where does the user must type is answer ?

i think that part you have already in you code, but i can not read?
if you like it just combine the two things.

<float age;
String prenom;

String Message_accueil=(“Bienvenue dans notre quizz nutritique 2019 !”);

void setup() {
size(700,700);
textSize(15);
println(Message_accueil); // Affiche un message d’acuueil dans la console

prenom=input.readString(“Entrez votre prénom”);
age=input.readFloat(“Entrez votre age ( en années )”);

println();
println(“Prénom :”);
println(prenom);
println();
println(“Age :”);
println(age);
}

String[] q1 = {
“Question 1 : Un repas chez des amis est pour vous \n\n 1_ Je mange mais je vais me rattraper demain\n\n 2_Je vais voir mes amis, le repas n’a aucune importance\n\n 3_ Un moment de plaisir à tous les niveaux”,
};
int point=0;

void draw () {
background(255,255,255);
fill(0,0,0);
text(q1[point], 150, 150);

q1=input.readString(“Votre choix parmis les 3 réponses possibles”);>

I can’t ask the user about the answer !

you could build on this really simple example if you didn’t want to use a UI library. It would save the user having to type out there answers and dealing with all the problems with that instead you can have a number of radio buttons which the user can select from. just add a button to move to the next question etc etc.

class RadioButton
{
  String label;
  float x, y, radius;
  boolean selected;
  
  public RadioButton(String label, float x, float y, float radius) {
    this.label = label;
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.selected = false;
  }
  
  Boolean select(float mx, float my) {
    float dx = x - mx;
    float dy = y - my;
    float d = dx * dx + dy * dy;
    if(d < radius * radius) {
      return selected = true;
    }
    return false;
  }
  
  void draw() {
    stroke(32);
    noFill();
    circle(x, y, radius);
    if(selected) {
      fill(32);
      circle(x, y, radius / 2);
    }
 
    fill(32);
    textSize(16);
    text(label, x - textWidth(label) / 2, y - 16);
  }
}

class QuestionHandler
{
  String question;
  String[] answers;
  RadioButton[] radioButtons;
  
  public QuestionHandler(String question, String[] answers) {
    this.question = question;
    this.answers = new String[answers.length];
    arrayCopy(answers, 0, this.answers, 0, answers.length);
    radioButtons = new RadioButton[answers.length];
    float radioButtonRadius = getRadioButtonSpacing(answers);
    float startX = (width / 2) - (radioButtons.length * radioButtonRadius / 2) + radioButtonRadius / 2;
    for(int i = 0; i < answers.length; i++) {
      radioButtons[i] = new RadioButton(answers[i], startX + i * radioButtonRadius, height / 2, 16);
    }
  }
  
  float getRadioButtonSpacing(String[] answers) {
    float rad = -10000;
    for(int i = 0; i < answers.length; i++) {
      float tw = textWidth(answers[i]);
      if(tw > rad)
        rad = tw;
    }
    return rad * 1.5;
  }
  
  void select(float mx, float my) {
    Boolean updatedSelection = false;
    for(int i = 0; i < radioButtons.length; i++) {
      RadioButton rb = radioButtons[i];
      
      if(rb.select(mx, my) && !updatedSelection) {
        updatedSelection = true;
      }
      else {
        rb.selected = false;
      }
    }
  }
  
  void draw() {
    fill(0);
    text(question, width / 2 - textWidth(question) / 2, height / 4);
    for(int i = 0; i < radioButtons.length; i++) {
      RadioButton rb = radioButtons[i];
      rb.draw();
    }
  }
}

QuestionHandler[] questionHandlers;
int currentQuestion;

void setup() {
  size(640, 480, P2D);
  
  questionHandlers = new QuestionHandler[] {
    new QuestionHandler("Is purple a fruit?", new String[] { "YES", "NO" }),
    new QuestionHandler("Which number is larger?", new String[] { "0", "46", "103234" })
  };
  currentQuestion = 0;
}

void draw() {
  background(200);
  textSize(32);

   questionHandlers[currentQuestion].draw();
}

void mouseReleased() {
  questionHandlers[currentQuestion].select(mouseX, mouseY);
}

Well thanks for reply but I don’t think that I’ll use this way, I have to present it during exams and I must explain everything so it musn’t be too hard, I have only few months of knowledge…

Is there any simple way to get what I want ?

By the way, after getting the answer, I’d like to get a number of points per answer…

I mean

QUESTION 1
A gives 1 points
B gives 2 pts
etc…
But I don’t remember how to do with if and else if

i’m guessing you can’t use libraries either then… bummer.

i’d setup a score variable and add to it when a user gets an answer correct which can be checked with if/else statements.

where you got that from? is that working?
but if not, why you have 3 lines of it?

possibly you just start with

the idea is you can
( after you focused the canvas with mouse click )
type on keyboard and get ( or aggregate ) the input to a variable,
and as the answers are a b or c that part is easy.
while the part you call ?readFloat? is not that easy.

1 Like

Hi,

If you want it really simple, you can use the keyReleased() function to get the input of the person.

Something like this:

String[] question;
int qNb;

void setup() {
  size(1200, 800);

  question = new String[4];

  question[0] = "Question 1 \n \n 1 - Answer 1 \n 2 - Answer 2";
  question[1] = "Question 2 \n \n 1 - Answer 1 \n 2 - Answer 2";
  question[2] = "Question 3 \n \n 1 - Answer 1 \n 2 - Answer 2";
  question[3] = "Question 4 \n \n 1 - Answer 1 \n 2 - Answer 2";

  qNb = 0;
}


void draw() {
  background(20);
  fill(200);
  textAlign(CENTER, CENTER);
  textSize(100);
  if (qNb < 4) {
    text(question[qNb], width / 2, height / 2);
  }
}


void keyReleased() {
  if (qNb == 0) {
    if (keyCode == 97) {
      println("Question 1 - Answer 1");
      qNb = 1;
    } else if (keyCode == 98) {
      println("Question 1 - Answer 2");
      qNb = 1;
    }
  } else if (qNb == 1) {
    if (keyCode == 97) {
      println("Question 2 - Answer 1");
      qNb = 2;
    } else if (keyCode == 98) {
      println("Question 2 - Answer 2");
      qNb = 2;
    }
  } else if (qNb == 2) {
    if (keyCode == 97) {
      println("Question 3 - Answer 1");
      qNb = 3;
    } else if (keyCode == 98) {
      println("Question 3 - Answer 2");
      qNb = 3;
    }
  } else if (qNb == 3) {
    if (keyCode == 97) {
      println("Question 4 - Answer 1");
      qNb = 4;
    } else if (keyCode == 98) {
      println("Question 4 - Answer 2");
      qNb = 4;
    }
  }
}

And this way you can also compute a score =)

1 Like

I tried everything you said,

So I’ve got this

void keyReleased() {
if (q1==2){
println(“Goob job !”);
}
}

Console says “Incompatible operand types String and int”

In your code, q1 is an array of String.

String[] q1 = {...

On this line: if (q1==2) you try to compare a String with a number. You can’t do that and that’s why the console is insulting you =)

you need to work on your skills

it requires that you learn debugging and looking up stuff in the reference

this is very basic stuff

when you take an exam you must be able to show us your own prototype

we can’t spoonfeed you

I know that I’m noob :frowning:
@jb4x How can I resolve that ?

read the tutorial about arrays

When you do if (q1==2), I presume that you try to check if the user chose the second answer.

If that’s the case you missed the idea of the exemple I gave you.

The keyReleased() function is called everytime the user is releasing a key. The key that the user just released is stored in a variable called keyCode.

So what you want to check is that the user did press the number on the keyboard that correspond to the answer that you were expected.

A bit of reading for you:
https://processing.org/reference/keyReleased_.html

1 Like

@jb4x So 2 must be always the right answer ?

I suck :frowning:

As @Chrisir said, you need to work a bit by yourself on this. Nobody will give you the answer.

You have enough exemple to figure it out by yourself.

Take some times, re-read everything, do some trial and error et come back later with some code to see where you are still stuck on.

1 Like