# Any idea how to fix this? Quiz

**URL:** <https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [November 12, 2019, 10:15am UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418 "2019-11-12T10:15:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CyberDodo](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@CyberDodo](https://discourse.processing.org/u/CyberDodo)\
**Post date:** [November 12, 2019, 10:15am UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418/1 "2019-11-12T10:15:15Z")

</div>

![buggy](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f27e40ba72a82a4be9a8944324485e450afbefb4.png)

```auto
int flag = 0;
void setup() {
size( 1366, 768);
background(255);
}
void draw() {
    if(flag == 0){
      push();
      background(255);
      fill(255, 0, 0);
      rect (700, 300, 300, 300);
      fill(0, 255, 0);
      rect (300, 300, 300, 300);
        fill(0, 0, 0);
         text("1. Aussage:", 600,150);
         text("Wir haben einen nicht gelisteten Helfer.", 350,190);
         textSize(32);
         fill(0, 51, 0);
         text("WAHR", 400, 450);
         textSize(32);
         fill( 128, 0, 0);
         text("FALSCH", 800, 450);
      pop();
    } else if (flag == 1) {
      push();
       background(255);
        fill(0, 255, 0);
        textSize(42);
        text("Richtig!", 600,150);
        fill(0);
        rect(600, 300, 300, 300);
         textSize(32);
        fill(255);
        text("Nächste", 700, 400);
        text("Aussage->", 700, 450);
        pop();    
   } else if (flag == 2) {
     push();
      background(255);
     fill(255, 0, 0);
     text("Falsch!", 600,150);
      fill(0);
        rect(600, 300, 300, 300);
         textSize(32);
        fill(255);
        text("Nächste", 700, 400);
        text("Aussage->", 700, 450);
     pop();
   } else if (flag == 3) {
     push();
     fill(255, 0, 0);
      rect (700, 300, 300, 300);
      fill(0, 255, 0);
      rect (300, 300, 300, 300);
        fill(0, 0, 0);
         text("2. Aussage:", 600,150);
         text("Wir sind das erste Mal dabei.", 350,190);
         textSize(32);
         fill(0, 51, 0);
         text("WAHR", 400, 450);
         textSize(32);
         fill( 128, 0, 0);
         text("FALSCH", 800, 450);
         pop();
     }
}

void mousePressed(){
  println("MouseX: " + mouseX + " MouseY: " + mouseY + " Flag: " + flag);
  if(flag == 0){
  if(mouseX>300 && mouseX < 600 && mouseY >300 && mouseY <600){
    flag = 1;
    }
  if(dist(mouseX,mouseY,50,50)<25){
    flag = 2;
  }
  }
  if(flag == 1){
  if(mouseX>600 && mouseX < 900 && mouseY >300 && mouseY <600){
    flag = 3;
  }
  } if(flag == 0){
  if(mouseX>700 && mouseX < 1000 && mouseY >300 && mouseY <600){
    flag = 2;
  }
  }
  if (flag == 2)
  if(mouseX>300 && mouseX < 600 && mouseY >300 && mouseY <600){
    flag = 3;
  }}

```

---

<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:** [November 12, 2019, 10:24am UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418/2 "2019-11-12T10:24:18Z")

</div>

fix WHAT exactly?

- you have / draw 4 pages
- and want jump between them by mouse click on hardcoded buttons?

better error description please

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 12, 2019, 11:15am UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418/3 "2019-11-12T11:15:43Z")

</div>

Hello

Is this a homework or academic assignment?

Is it a new topic or related to your last topic? They look similar.

> [@How can I change the page by clicking a button?](https://discourse.processing.org/t/how-can-i-change-the-page-by-clicking-a-button/15244):
>
> How can I change the page by clicking a button?

A clear statement of the problem helps.  
Add some comments to your code.

Consider drawing a “state diagram”:  
[State Diagram](https://www.google.com/search?q=state+diagram&rlz=1C1CHBF_enCA822CA822&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjSgJSQxeTlAhUnVt8KHfUBBiUQ_AUIEigB&biw=1280&bih=705)  
I use state diagrams and flowcharts for my more complex code.

Hint to get you started:  
Try putting `flag = 0;` at the end of if statements in the main loop; try one at a time and observe.

That is it for now…

🙂

---

<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:** [November 12, 2019, 11:55am UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418/4 "2019-11-12T11:55:42Z")

</div>

I remarked in his last thread that arrays would be the way to go

But he‘s doing a good job!

---

<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:** [November 12, 2019, 12:16pm UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418/5 "2019-11-12T12:16:18Z")

</div>

> [@Chrisir](#):
>
> arrays would be the way to go

but even only button variables and a over function would be a big advancement, see  
[Background(image) not working - #74 by Chrisir](https://discourse.processing.org/t/background-image-not-working/14305/74) ,

---

<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:** [November 12, 2019, 9:07pm UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418/6 "2019-11-12T21:07:51Z")

</div>

here is a working version. There were a lot of small things to fix.

(as I said: Arrays)

Chrisir

```auto

int flag = 0;

void setup() {
  size( 1366, 768);
  background(255);
}//func 

void draw() {
  background(255);
  actOnFlag();
}//func 

// -------------------------------------------------

void actOnFlag() {
  if (flag == 0) {
    // Question 1
    pushStyle();
    background(255);
    fill(255, 0, 0);
    rect (700, 300, 300, 300);
    fill(0, 255, 0);
    rect (300, 300, 300, 300);
    fill(0, 0, 0);
    text("1. Aussage:", 600, 150);
    text("Wir haben einen nicht gelisteten Helfer.", 350, 190);
    textSize(32);
    fill(0, 51, 0);
    text("WAHR", 400, 450);
    textSize(32);
    fill( 128, 0, 0);
    text("FALSCH", 800, 450);
    popStyle();
  } else if (flag == 1) {
    // Correct 
    pushStyle();
    background(255);
    fill(0, 255, 0);
    textSize(42);
    text("Richtig!", 600, 150);
    fill(0);
    rect(600, 300, 300, 300);
    textSize(32);
    fill(255);
    text("Nächste", 700, 400);
    text("Aussage->", 700, 450);
    popStyle();
  } else if (flag == 2) {
    // Wrong 
    pushStyle();
    background(255);
    fill(255, 0, 0);
    text("Falsch!", 600, 150);
    fill(0);
    rect(600, 300, 300, 300);
    textSize(32);
    fill(255);
    text("Nächste", 700, 400);
    text("Aussage->", 700, 450);
    popStyle();
  } else if (flag == 3) {
    // Question 2
    pushStyle();
    background(255);
    fill(255, 0, 0);
    rect (700, 300, 300, 300);
    fill(0, 255, 0);
    rect (300, 300, 300, 300);
    fill(0, 0, 0);
    text("2. Aussage:", 600, 150);
    text("Wir sind das erste Mal dabei.", 350, 190);
    textSize(32);
    fill(0, 51, 0);
    text("WAHR", 400, 450);
    textSize(32);
    fill( 128, 0, 0);
    text("FALSCH", 800, 450);
    popStyle();
  } else {
    background(255);
    fill(255, 0, 0);
    text("Else: "+flag, 600, 150);
  }
}//func 

// -------------------------------------------------

void mousePressed() {
  println("MouseX: " + mouseX + " MouseY: " + mouseY + " Flag: " + flag);

  if (flag == 0) {
    // Question 1
    if (mouseX>300 && mouseX < 600 && mouseY >300 && mouseY <600) {
      flag = 1;
    }
    if (dist(mouseX, mouseY, 849, 450)<125) {
      flag = 2;
    }
  }
  //------------------------
  // Two result screens with a "Next button"
  else if (flag == 1) {
    // Correct 
    if (mouseX>600 && mouseX < 900 && mouseY >300 && mouseY <600) {
      flag = 3; // both proceed to Question 2
    }
  } else if (flag == 2) {
    // Wrong 
    if (mouseX>600 && mouseX < 900 && mouseY >300 && mouseY <600) {
      flag = 3; // both proceed to Question 2
    }
  } 
  //---------------------
  else if (flag == 3) {
    // Question 2
    if (mouseX>300 && mouseX < 600 && mouseY >300 && mouseY <600) {
      flag = 4;
    }
    if (dist(mouseX, mouseY, 850, 450)<125) {
      flag = 5;
    }
  }//if
  //--------------------
  else {
    // flag = 0;
  }
  //
}//func 
//

```

---

<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:** [November 12, 2019, 9:51pm UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418/7 "2019-11-12T21:51:14Z")

</div>

here is an array version

```auto

int flag = 0;

// 3 parallel arrays 

String[] text1 = { 
  "1. Aussage:", 
  "2. Aussage:", 
  "3. Aussage:", 
  "4. Aussage:"
};

String[] text2 = { 
  "Wir haben einen nicht gelisteten Helfer.", 
  "Wir sind das erste Mal dabei.", 
  "<3>", 
  "<4>"
};

int[] correct = {
  1, 
  2, 
  1, 
  2
};

// index for all 3 
int index = 0; 

void setup() {
  size( 1366, 768);
  background(255);
}//func 

void draw() {
  background(255);
  actOnFlag();
}//func 

// -------------------------------------------------

void actOnFlag() {
  if (flag == 0) {
    // Question 1
    pushStyle();
    background(255);
    fill(255, 0, 0);
    rect (700, 300, 300, 300);
    fill(0, 255, 0);
    rect (300, 300, 300, 300);
    fill(0, 0, 0);
    text(text1[index], 600, 150);
    text(text2[index], 350, 190);
    textSize(32);
    fill(0, 51, 0);
    text("WAHR", 400, 450);
    textSize(32);
    fill( 128, 0, 0);
    text("FALSCH", 800, 450);
    popStyle();
  } else if (flag == 1 || flag == 2) {  
    //
    resultScreen();
  } else if (flag == 3) {
    // go on
    flag=0;
    index++;
  } else {
    background(255);
    fill(255, 0, 0);
    text("Error in actOnFlag: "+flag, 600, 150);
  }
}//func 

void resultScreen() {
  if ((flag == 1 && correct[index] == 1) || (flag == 2 && correct[index] == 2)) {
    // Correct 
    pushStyle();
    background(255);
    fill(0, 255, 0);
    textSize(42);
    text("Richtig!", 600, 150);
    fill(0);
    rect(600, 300, 300, 300);
    textSize(32);
    fill(255);
    text("Nächste", 700, 400);
    text("Aussage->", 700, 450);
    popStyle();
  } else {
    // Wrong 
    pushStyle();
    background(255);
    fill(255, 0, 0);
    text("Falsch!", 600, 150);
    fill(0);
    rect(600, 300, 300, 300);
    textSize(32);
    fill(255);
    text("Nächste", 700, 400);
    text("Aussage->", 700, 450);
    popStyle();
  }
}//

// -------------------------------------------------

void mousePressed() {
  println("MouseX: " + mouseX + " MouseY: " + mouseY + " Flag: " + flag);

  //------------------------
  if (flag == 0) {
    // Question 1
    if (mouseX>300 && mouseX < 600 && mouseY >300 && mouseY <600) {
      flag = 1;
    }
    if (dist(mouseX, mouseY, 849, 450)<125) {
      flag = 2;
    }
  }
  //------------------------
  // Two result screens with a "Next button"
  else if (flag == 1) {
    // Correct 
    if (mouseX>600 && mouseX < 900 && mouseY >300 && mouseY <600) {
      flag = 3; // both proceed to Question 2
    }
  } else if (flag == 2) {
    // Wrong 
    if (mouseX>600 && mouseX < 900 && mouseY >300 && mouseY <600) {
      flag = 3; // both proceed to Question 2
    }
  } 
  //---------------------
  else if (flag == 3) {
    //
  }//if
  //--------------------
  else {
    // flag = 0;
  }
  //
}//func 
//

```

---

<div class="post-metadata">

**Author:** ![CyberDodo](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@CyberDodo](https://discourse.processing.org/u/CyberDodo)\
**Post date:** [November 14, 2019, 4:52pm UTC](https://discourse.processing.org/t/any-idea-how-to-fix-this-quiz/15418/8 "2019-11-14T16:52:05Z")

</div>

Thanks, this is an much better option.
