Help with user input dialogues

Hello again,
I’m making a “Geometry tutor” program which contains instructions, learn, and a practice section where the user can quiz themselves. I have been instructed to use a specific input file for the quiz questions (It is below my code). There are 10 questions in easy(), and after the tenth question the program should return to mainMenu(). Which it does, but the program continues to have input dialogue boxes pop up again. I can’t get rid of them. I have tried noLoop() and loop(), but these do not work.

What should I do to stop the question boxes from appearing when in a separate method? Any advice/tips will be appreciated.

mainMenu -> practice -> easy

String state="splashScreen";
int x=1;
int pointX, pointY;
int score=0;
String e1, e2, e3, e4, e5, e6, e7, e8, e9, e10;

void setup() {
  size(500, 500);
  mainMenu();
  noStroke();

}

void mousePressed() {
  if (state.equals("mainMenu")) {
    if (pointX >=200 && pointX <= 300 && pointY >= 200 && pointY <= 220) {
      state="instructions";
    } else if (pointX >=200 && pointX <= 300 && pointY >= 240 && pointY <= 260) {
      state="learn";
    } else if (pointX >= 200 && pointX <= 300 && pointY >= 280 && pointY <= 300) {
      state="practice";
    } else if (pointX >= 200 && pointX <= 300 && pointY >= 320 && pointY <= 340) {
      state="goodbye";
    }
  }
  
  if (state.equals("practice")) {
    if (pointX >= 80 && pointX <=180 && pointY >= 200 && pointY <= 220) {
      state="easy";
     } else if (pointX >= 200 && pointX <= 300 && pointY >=200 && pointY <= 220) {
       state="medium";
     } else if (pointX >= 320 && pointX <= 420 && pointY >= 200 && pointY <= 220) {
       state="hard";
     }
  }
  
  
  if (state.equals("goodbye")) {
    if (pointX >= 200 && pointX <= 300 && pointY >= 440 && pointY <= 460) {
      state="exit";
    }
  }
}

void mainMenu() {
  pointX=mouseX;
  pointY=mouseY;

  fontD=loadFont("fontD.vlw");
  textFont(fontD);

  background(255);
  white=loadImage("white.jpg");
  white.resize(0, 900);
  image(white, 0, 0);
  
  textSize(40);
  text("Geometry Tutor", 100, 100);
  textSize(20);
  text("Main Menu", 195, 130);

  fontE=loadFont("fontE.vlw");
  textFont(fontE);

  //first choice
  rectMode(CORNERS);
  rect(200, 200, 300, 220);
  textSize(20);
  fill(255);
  textSize(15);
  text("Instructions", 202, 215);

  //second choice
  fill(#6894EA);
  rect(200, 240, 300, 260);
  textSize(20);
  fill(255);
  textSize(15);
  text("Learn", 230, 255);

  //third choice 
  fill(#6894EA);
  rect(200, 280, 300, 300);
  textSize(20);
  fill(255);
  textSize(15);
  text("Practice", 220, 295);

  //fourth choice 
  fill(#6894EA);
  rect(200, 320, 300, 340);
  textSize(20);
  fill(255);
  textSize(15);
  text("Exit", 235, 335);
}

void easy() {
  
  String e1=getString("True or false: a square has 5 right angles");
  e1=e1.toLowerCase();
  if (e1.equals("false")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  
  String e2=getString("Which shape has an interior angle sum of 180 degrees?");
  e2=e2.toLowerCase();
  if (e2.equals("triangle")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  String e3=getString("If a square's perimeter is 64, what is its area?");
  if (e3.equals("256")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  String e4=getString("True or false: a square is a special kind of rectangle.");
  e4=e4.toLowerCase();
  if (e4.equals("true")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  String e5=getString("How many sides does a circle have?");
  e5=e5.toLowerCase();
  if (e5.equals("none") || e5.equals(0)) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  String e6=getString("If a triangle's base is 9 and its height is 8, what is its area?");
  if (e6.equals("36")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  String e7=getString("If a rhombus's side length is 6, what is its perimeter?");
  if (e7.equals("24")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  String e8=getString("A trapezoid has height 5, bottom length 10, and top length 8. What is its area?");
  if (e8.equals("45")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  String e9=getString("True or false: Parallelograms are rhombuses.");
  if (e9.equals("false")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
  }
  String e10=getString("If a rhombus's side length is 3 and its height is 10, what is its area?");
  if (e10.equals("15")) {
    score+=1;
    println("You are correct!");
    println("Your score is: " + score);
    mainMenu();
  } else {
    println("Oops! That was not correct.");
    println("Your score is: " + score);
    mainMenu();
  }
  
  }

void draw() {
switch(state) {
  case "splashScreen":
        splashScreen();
        break; 
  case "mainMenu":
        mainMenu();
        break; 
  case "practice"
          practice();
          break();
case  "easy"
           easy();
           break;
}
`

Here is the input file we use:

import javax.swing.*;

String prompt(String s)
{
   println(s);
   String entry = JOptionPane.showInputDialog(s);
   if (entry == null)
      return null;
   println(entry);
   return entry;
}

String getString(String s)
{
   return prompt(s);
}

int getInt(String s)
{
   return Integer.parseInt(getString(s));
}

long getLong(String s)
{
   return Long.parseLong(getString(s));
}

float getFloat(String s)
{
   return Float.parseFloat(getString(s));
}

double getDouble(String s)
{
   return Double.parseDouble(getString(s));
}

char getChar(String s)
{
   String entry = prompt(s);
   if (entry.length() >= 1)
      return entry.charAt(0);
   else
      return '\n';
}
2 Likes

Hey There!

Wowe that alot of buttons with hardcode ! Did you consider making a button class of some sorts / menus to simplify and minimize error in your program ?

1 Like