True and false keycodes for game (Quiz / true or false game)

I am a new learner in coding, and i can’t figure out how to make this true and false game work. if it is possible can someone help me with making a keycode ‘t’ for true and keycode ‘f’ for false? thanks!

String myText;
int totalWrong = 0;
int totalCorrect = 0;
int question = 0;
String correctAnswer;
String WrongAnswer;
int True= totalCorrect++;
int False= totalWrong++;

void setup() { 

  size(800, 800);
  fill(0);
  myText = "Welcome to the Quiz Game";
  textSize(40);
}


void draw() {
  background(255);


  //Set up the screen for the user. 
  text("", 0, 300, 800, 700);
  text(question, 20, 80);
  text("Correct:", 600, 40);
  text(totalCorrect, 620, 80);
  text("Incorrect:", 600, 140);
  text(totalWrong, 620, 180);


  //Display the user input text


  text(myText, 50, 650);


  //OPTIONAL: To improve your code: maybe make a "displayQuestion function.


// ---------------------------SET UP THE QUIZ HERE -----------------------------


  if (question ==0) {
    //Intro 
    //Put some user facing (external) documentation here.
    text("Press Enter to Begin", 50, 250, 700, 250);
    correctAnswer = "";
  }
  else if (question == 1){
   
    text("Question:lightning never strikes in the same place twice. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";

  }
  else if (question == 2){

    text("Question: if you cry in space, the tears just stick onto your face. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";

  }
  else if (question == 3){

    text("Question: If you cut an earthworm in half, both halves can regrow their body. True or False? ", 50, 250, 700, 250);
    correctAnswer = "False"; 

  }
  else if (question == 4){
   
    text("Question: Humans can distinguish between over a trillion different smells. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
    
  }
  else if (question == 5){
   
    text("Question: Adults have fewer bones than babies do. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
    
  }else if (question == 6){
   
    text("Question: Goldfish only have a memory span of 3 seconds.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
    
  }else if (question == 7){
   
    text("Question: there are more cells of bacteria in your body than there are human cells.True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
    
  }else if (question == 8){
   
    text("Question: Your fingernails and hair keep growing after you die. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
    
  }else if (question == 9){
   
    text("Question: Water spirals down the plughole in opposite directions in the northern and southern hemispheres.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
    
  }else if (question == 10){
   
    text("Question: Humans can't breathe and swallow at the same time. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
    
  }else if (question == 4){
   
    text("Question: A penny dropped from a skyscraper can reach sufficient velocity to kill a pedestrian below. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  }

  else{
   text("You are done!", 50,250,700,250);
   text("You got " + totalCorrect + " correct answers.", 50,300,700,250);
   
  
  }
  
  
  //---------------------------------------------------------------------------
}

//Use this function to enter text into the myText string.
void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (myText.length() > 0) {
      myText = myText.substring(0, myText.length()-1);
    }
  } else if (keyCode == DELETE) {
    myText = "";
  } else if ( keyCode == ENTER) {

    //What happens when you press ENTER
    checkAnswer(myText, correctAnswer);
    myText = "";
    
    
  } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
    myText = myText + key;
  }
else if (keyCode == 't'){
  totalCorrect++;
}else if (keyCode == 'f'){
  totalWrong++;
}
} 

void checkAnswer(String answer, String correctAnswer) {

  answer = answer.toLowerCase();
  correctAnswer = correctAnswer.toLowerCase();

  if (question!=0) {

    //Note: You could use .contains() instead.
    if (answer.equals(correctAnswer)) {

      totalCorrect++;
    } else { 

      totalWrong++;
    }
  }
  question++;
}

Please format your Code correctly, and please refrase your question to make it clear what you‘re having problems with.

Hey, and welcome to the forum!

Great to have you here!

Please learn how to post code

This is making a keycode ‘t’ for true and keycode ‘f’ for false

But you need to check whether true or false is the correct answer for this current question

Chrisir



String myText;
int totalWrong = 0;
int totalCorrect = 0;
int question = 0;
String correctAnswer;
String WrongAnswer;

int True= totalCorrect++;
int False= totalWrong++;

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

void setup() {
  size(800, 800);
  fill(0);
  myText = "Welcome to the Quiz Game";
  textSize(40);
}

void draw() {
  background(255);

  //Set up the screen for the user.
  text("", 0, 300, 800, 700);
  text(question, 20, 80);
  text("Correct:", 600, 40);
  text(totalCorrect, 620, 80);
  text("Incorrect:", 600, 140);
  text(totalWrong, 620, 180);

  //Display the user input text

  text(myText, 50, 650);

  //OPTIONAL: To improve your code: maybe make a "displayQuestion function.

  // ---------------------------SET UP THE QUIZ HERE -----------------------------

  if (question ==0) {
    //Intro
    //Put some user facing (external) documentation here.
    text("Press Enter to Begin", 50, 250, 700, 250);
    correctAnswer = "";
  } else if (question == 1) {

    text("Question:lightning never strikes in the same place twice. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 2) {

    text("Question: if you cry in space, the tears just stick onto your face. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 3) {

    text("Question: If you cut an earthworm in half, both halves can regrow their body. True or False? ", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 4) {

    text("Question: Humans can distinguish between over a trillion different smells. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 5) {

    text("Question: Adults have fewer bones than babies do. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 6) {

    text("Question: Goldfish only have a memory span of 3 seconds.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 7) {

    text("Question: there are more cells of bacteria in your body than there are human cells.True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 8) {

    text("Question: Your fingernails and hair keep growing after you die. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 9) {

    text("Question: Water spirals down the plughole in opposite directions in the northern and southern hemispheres.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 10) {

    text("Question: Humans can't breathe and swallow at the same time. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 4) {

    text("Question: A penny dropped from a skyscraper can reach sufficient velocity to kill a pedestrian below. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else {
    text("You are done!", 50, 250, 700, 250);
    text("You got " + totalCorrect + " correct answers.", 
      50, 300, 
      700, 250);
  }

  //---------------------------------------------------------------------------
}

//Use this function to enter text into the myText string.
void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (myText.length() > 0) {
      myText = myText.substring(0, myText.length()-1);
    }
  } else if (keyCode == DELETE) {
    myText = "";
  } else if ( keyCode == ENTER ) {
    //What happens when you press ENTER
    checkAnswer(myText, correctAnswer);
    myText = "";
  } else if (key == 't') {
    println("t");
    totalCorrect++;
    checkAnswer(myText, correctAnswer);
  } else if (key == 'f') {
    totalWrong++;
    checkAnswer(myText, correctAnswer);
  } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
    myText = myText + key;
  }
}

void checkAnswer(String answer, String correctAnswer) {

  answer = answer.toLowerCase();
  correctAnswer = correctAnswer.toLowerCase();

  if (question!=0) {

    //Note: You could use .contains() instead.
    if (answer.equals(correctAnswer)) {

      totalCorrect++;
    } else { 

      totalWrong++;
    }
  }
  question++;
}
//
1 Like

Need a piece of code where keycode ‘t’ is equal to true and ‘f’ is equal to false. And also if the questions answer is equal to false, and the f keycode is pressed, they should get one correct answer but if they press the t keycode, they should get one wrong in the game, and vice versa.

Hey, Zayba, this description is much better than what you had before.

I showed you how to read f and t.

And I put totalCorrect++; there what was just placeholder really.

Replace it with something like

if ( correctAnswer.equals("False")  )
   totalCorrect++;
else totalWrong++;

and for ‘t’ the other way round

Then show your entire code

Chrisir

Btw, the Code has an error at the last question, where (question == 4 ) is a duplicate.

String myText;
int totalWrong = 0;
int totalCorrect = 0;
int question = 0;
String correctAnswer;
String WrongAnswer;

int True= totalCorrect++;
int False= totalWrong++;

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

void setup() {
  size(800, 800);
  fill(0);
  myText = "Welcome to the Quiz Game";
  textSize(40);
}

void draw() {
  background(255);

  //Set up the screen for the user.
  text("", 0, 300, 800, 700);
  text(question, 20, 80);
  text("Correct:", 600, 40);
  text(totalCorrect, 620, 80);
  text("Incorrect:", 600, 140);
  text(totalWrong, 620, 180);

  //Display the user input text

  text(myText, 50, 650);

  //OPTIONAL: To improve your code: maybe make a "displayQuestion function.

  // ---------------------------SET UP THE QUIZ HERE -----------------------------

  if (question ==0) {
    //Intro
    //Put some user facing (external) documentation here.
    text("Press Enter to Begin", 50, 250, 700, 250);
    correctAnswer = "";
  } else if (question == 1) {

    text("Question:lightning never strikes in the same place twice. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 2) {

    text("Question: if you cry in space, the tears just stick onto your face. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 3) {

    text("Question: If you cut an earthworm in half, both halves can regrow their body. True or False? ", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 4) {

    text("Question: Humans can distinguish between over a trillion different smells. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 5) {

    text("Question: Adults have fewer bones than babies do. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 6) {

    text("Question: Goldfish only have a memory span of 3 seconds.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 7) {

    text("Question: there are more cells of bacteria in your body than there are human cells.True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 8) {

    text("Question: Your fingernails and hair keep growing after you die. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 9) {

    text("Question: Water spirals down the plughole in opposite directions in the northern and southern hemispheres.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 10) {

    text("Question: Humans can't breathe and swallow at the same time. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 11) {

    text("Question: A penny dropped from a skyscraper can reach sufficient velocity to kill a pedestrian below. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else {
    text("You are done!", 50, 250, 700, 250);
    text("You got " + totalCorrect + " correct answers.", 
      50, 300, 
      700, 250);
  }

  //---------------------------------------------------------------------------
}

//Use this function to enter text into the myText string.
void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (myText.length() > 0) {
      myText = myText.substring(0, myText.length()-1);
    }
  } else if (keyCode == DELETE) {
    myText = "";
  } else if ( keyCode == ENTER ) {
    //What happens when you press ENTER
    checkAnswer(myText, correctAnswer);
    myText = "";
  } else if (key == 't') {
    println("t");
    totalCorrect++;
    checkAnswer(myText, correctAnswer);
  } else if (key == 'f') {
    totalWrong++;
    checkAnswer(myText, correctAnswer);
  } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
    myText = myText + key;
  }
}

void checkAnswer(String answer, String correctAnswer) {

  answer = answer.toLowerCase();
  correctAnswer = correctAnswer.toLowerCase();

  if (question!=0) {

    //Note: You could use .contains() instead.
    if (answer.equals(correctAnswer)) {

      if ( correctAnswer.equals("False")  )
   totalCorrect++;
else totalWrong++;
    }else{
      if ( correctAnswer.equals("True")  )
   totalCorrect++;
else totalWrong++;

      totalWrong++;
    }
  }
  question++;
}
//

this is my code now, but there is still something wrong it because when I press key-code ‘f’, or ‘t’, the score jumps by 1 or 2 for each of the scores.

![zayba4|690x462]

1 Like

Never mind, i figured out how to get the scores to go up by one, but if the answer to question one is false, and i press the ‘f’ key then the counter adds one to the incorrect group instead of the correct answer group. And vice versa. I really appreciate the help!

Did you post the last / current version of the Sketch or is the above version old?

Maybe your problem lies in these lines.

Can you check whether they are correct?

Please, in processing hit ctrl-t to get auto-format

String myText;
int totalWrong = 0;
int totalCorrect = 0;
int question = 0;
String correctAnswer;
String WrongAnswer;

int True= totalCorrect++;
int False= totalWrong++;

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

void setup() {
  size(800, 800);
  fill(0);
  myText = "Welcome to the Quiz Game";
  textSize(40);
}

void draw() {
  background(255);

  //Set up the screen for the user.
  text("", 0, 300, 800, 700);
  text(question, 20, 80);
  text("Correct:", 600, 40);
  text(totalCorrect, 620, 80);
  text("Incorrect:", 600, 140);
  text(totalWrong, 620, 180);

  //Display the user input text

  text(myText, 50, 650);

  //OPTIONAL: To improve your code: maybe make a "displayQuestion function.

  // ---------------------------SET UP THE QUIZ HERE -----------------------------

  if (question ==0) {
    //Intro
    //Put some user facing (external) documentation here.
    text("Press Enter to Begin", 50, 250, 700, 250);
    correctAnswer = "";
  } else if (question == 1) {

    text("Question:lightning never strikes in the same place twice. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 2) {

    text("Question: if you cry in space, the tears just stick onto your face. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 3) {

    text("Question: If you cut an earthworm in half, both halves can regrow their body. True or False? ", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 4) {

    text("Question: Humans can distinguish between over a trillion different smells. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 5) {

    text("Question: Adults have fewer bones than babies do. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 6) {

    text("Question: Goldfish only have a memory span of 3 seconds.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 7) {

    text("Question: there are more cells of bacteria in your body than there are human cells.True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 8) {

    text("Question: Your fingernails and hair keep growing after you die. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 9) {

    text("Question: Water spirals down the plughole in opposite directions in the northern and southern hemispheres.True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else if (question == 10) {

    text("Question: Humans can't breathe and swallow at the same time. True or False?", 50, 250, 700, 250);
    correctAnswer = "True";
  } else if (question == 11) {

    text("Question: A penny dropped from a skyscraper can reach sufficient velocity to kill a pedestrian below. True or False?", 50, 250, 700, 250);
    correctAnswer = "False";
  } else {
    text("You are done!", 50, 250, 700, 250);
    text("You got " + totalCorrect + " correct answers.", 50, 300, 700, 250);
  }

  //---------------------------------------------------------------------------
}

//Use this function to enter text into the myText string.
void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (myText.length() > 0) {
      myText = myText.substring(0, myText.length()-1);
    }
  } else if (keyCode == DELETE) {
    myText = "";
  } else if ( keyCode == ENTER ) {
    //What happens when you press ENTER
    checkAnswer(myText, correctAnswer);
    myText = "";
  } else if (key == 't') {
    println("t");
    totalCorrect++;
    checkAnswer(myText, correctAnswer);
  } else if (key == 'f') {
    totalWrong++;
    checkAnswer(myText, correctAnswer);
  } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
    myText = myText + key;
  }
}

void checkAnswer(String answer, String correctAnswer) {

  answer = answer.toLowerCase();
  correctAnswer = correctAnswer.toLowerCase();

  if (question!=0) {

    //Note: You could use .contains() instead.
    if (answer.equals(correctAnswer)) {

      if ( correctAnswer.equals("False")  )
        totalCorrect++;
      else totalWrong++;
    } else {
      if ( correctAnswer.equals("True")  )
        totalCorrect--;
      else totalWrong--;

      totalWrong=0+1;
    }
  }
  question++;
}
//

this is my most recent code

i tried what you suggested, Chrisir, but the same problem pops up.

can somebody please check this, I don’t have time

Remove the incrementing in the keyPressed t and f functions. Generally, what are they used for? If you press enter, if the answer is right you get correctAnswer++, but if you press t and give in the wrong answer, you get correctAnswer++ and wrongAnswer++… you shouldn‘t set correctAnswer++, only because the t key is pressed.

Im assuming you intended to go with t for true and f for false as an answer and somewhere decided to have people write the answer out as a text and then decide wether it‘s the correct answer, but you can‘t use both at the same time (that just doesn’t make much sense, though you could technically do it…).

Im not sure how clear i‘m being right now, but in short, in KeyPressed, remove the correctAnswer++; and falseAnswer++; within the key = ‚t‘ and key = ‚f‘ parts. They cause the wrong values.

Note, i couldn‘t get the Code to run on IOS, so i can‘t tell if that solves it, but i think it should, if there‘s not another issue.

1 Like