# Quiz with multiples choice - Beginner level

**URL:** https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258
**Category:** Coding Questions
**Created:** [April 14, 2019, 10:23am UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258 "2019-04-14T10:23:19Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![EverybodyEthan](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EverybodyEthan](https://discourse.processing.org/u/EverybodyEthan)
#### Post date: [April 14, 2019, 10:23am UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/1 "2019-04-14T10:23:19Z")

</div>

Hi buddies ! 😃

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 ?

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 14, 2019, 10:25am UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/2 "2019-04-14T10:25:23Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![EverybodyEthan](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EverybodyEthan](https://discourse.processing.org/u/EverybodyEthan)
#### Post date: [April 14, 2019, 11:39am UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/3 "2019-04-14T11:39:21Z")

</div>

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  
\<

 ![try](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a9e54a597c562d35b366b6766afbd2c23ffe8961.png)\>

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 14, 2019, 11:48am UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/4 "2019-04-14T11:48:03Z")

</div>

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

```auto
</> 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.

```auto
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);
}

```

 ![SNAG-0073](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ed2dc35440d5d5289ca45ea079dbbcb1b97d1e38.jpeg)

---

<div class="post-metadata">

### Author: ![EverybodyEthan](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EverybodyEthan](https://discourse.processing.org/u/EverybodyEthan)
#### Post date: [April 14, 2019, 11:52am UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/5 "2019-04-14T11:52:55Z")

</div>

That is very interesting ! I appreciate the help !

But where does the user must type is answer ?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 14, 2019, 11:55am UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/6 "2019-04-14T11:55:16Z")

</div>

> [@EverybodyEthan](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![EverybodyEthan](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EverybodyEthan](https://discourse.processing.org/u/EverybodyEthan)
#### Post date: [April 14, 2019, 1:12pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/7 "2019-04-14T13:12:04Z")

</div>

\<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 !**

---

<div class="post-metadata">

### Author: ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)
#### Post date: [April 14, 2019, 2:34pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/8 "2019-04-14T14:34:19Z")

</div>

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.

```auto
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);
}

```

---

<div class="post-metadata">

### Author: ![EverybodyEthan](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EverybodyEthan](https://discourse.processing.org/u/EverybodyEthan)
#### Post date: [April 14, 2019, 2:40pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/9 "2019-04-14T14:40:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)
#### Post date: [April 14, 2019, 2:47pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/10 "2019-04-14T14:47:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 14, 2019, 2:58pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/11 "2019-04-14T14:58:10Z")

</div>

> [@EverybodyEthan](#):
>
> input.readString…

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

possibly you just start with

> **[keyPressed() / Reference](https://processing.org/reference/keyPressed_.html)**
>
> The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable. For non-ASCII keys, use the keyCode varia…

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.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 14, 2019, 2:58pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/12 "2019-04-14T14:58:15Z")

</div>

Hi,

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

Something like this:

```auto
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 =)

---

<div class="post-metadata">

### Author: ![EverybodyEthan](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EverybodyEthan](https://discourse.processing.org/u/EverybodyEthan)
#### Post date: [April 14, 2019, 4:11pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/13 "2019-04-14T16:11:42Z")

</div>

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”

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 14, 2019, 4:14pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/14 "2019-04-14T16:14:19Z")

</div>

In your code, `q1` is an array of String.

```auto
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 =)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 14, 2019, 4:15pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/15 "2019-04-14T16:15:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![EverybodyEthan](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EverybodyEthan](https://discourse.processing.org/u/EverybodyEthan)
#### Post date: [April 14, 2019, 4:20pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/16 "2019-04-14T16:20:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 14, 2019, 4:21pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/17 "2019-04-14T16:21:52Z")

</div>

read the tutorial about arrays

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 14, 2019, 4:30pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/18 "2019-04-14T16:30:56Z")

</div>

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](https://processing.org/reference/keyReleased_.html)

---

<div class="post-metadata">

### Author: ![EverybodyEthan](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EverybodyEthan](https://discourse.processing.org/u/EverybodyEthan)
#### Post date: [April 14, 2019, 4:37pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/19 "2019-04-14T16:37:30Z")

</div>

@jb4x So 2 must be always the right answer ?

I suck ☹

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 14, 2019, 4:40pm UTC](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258/20 "2019-04-14T16:40:22Z")

</div>

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.

[Next page](https://discourse.processing.org/t/quiz-with-multiples-choice-beginner-level/10258.md?page=2)
