Help with loops

Working on a homework assignment and I’m absolutely horrible with programming. I need to run a program that asks a riddle and lists some words. the Answer to the riddle is the first letter of each word. Once the user inputs the correct answer into JOptionPane the screen is supposed to turn black. The part that I’m hung up on is getting the user input to turn the screen black and I can’t seem to find any tutorial videos that will talk about this using strings. I found one that uses int’s but I’m at a loss as to how to do this with strings.

color boxFill =color(0);
color letterFill = color (0, 200, 0);
String input;

void setup()
{
  size (500, 500);
  textSize (40);
  rectMode (CORNERS);
  textAlign (CENTER);
}

void draw ()
{  
  background (100);
  opener ();
  clues();
  finalQuestion();  
  answerButton();
}

void opener ()
{
  String opener = "i am:  ";
  fill (boxFill);
  stroke (0);
  rect (width/2-textWidth(opener)/2, height/24+textAscent(), 
    width/2+textWidth(opener)/2, height/24-textDescent());
  fill (letterFill);

  opener = opener.toUpperCase();
  text (opener, width/2, height/10);
}

void clues()
{
  stroke (0);
  String clues = ("one, two, three, four ");
  clues = clues.toUpperCase();
  String [] list = clues.split(" ");
  for (int i = 0; i < list.length; i++)
  {
    fill (boxFill);
    rect (width/2-textWidth(list[i])/2, height/7+i*50+textAscent(), 
      width/2+textWidth(list[i])/2, height/7+i*50-textDescent());
    fill (letterFill);
    text (list[i], width/2, height/5+i*50);
  }
}

void finalQuestion ()
{
  String question = "Who am I?";
  stroke (0);
  fill (boxFill);
  rect (width/2-textWidth(question)/2, height-height/3-textAscent(), 
    width/2+textWidth(question)/2, height-height/3+textDescent());
  fill (letterFill);
  text (question, width/2, height-height/3);
}

void answerButton()
{
  String correct = "Correct";
  String answer = "Answer?";
  fill (255, 0, 0);
  stroke (255);
  rect (width/2-textWidth(answer)/2, height-height/5-textAscent(), 
    width/2+textWidth(answer)/2, height-height/5+textDescent());
  fill (255);
  text (answer, width/2, height-height/5);
  if (mousePressed && mouseX >= 200 && mouseX <= 300 && mouseY >= 300 && mouseY <= 400) 
  {    
    String solution = "";
    while (!solution.equals("OTTF"))
    {
      String input = javax.swing.JOptionPane.showInputDialog(null, answer);
      if  (input.equals("OTTF"))
      {
        background (0);
        noLoop();
      }
    }
  }
}

This line jumps out at me:

A couple things here:

First, you never want to use a single = equals sign in an if statement. That’s assigning a new value to the variable, not comparing the variable to the value.

Secondly, you actually (almost) never want to use == with String values at all. Instead, you want to use the equals() function. Here’s a simple example:

String one = "test";
String two = "test";

if(one.equals(two)){
  println("equal");
}

If I were you, I would try to work in smaller steps and test out the syntax in small example programs like the one I just posted. Then if you get stuck on one of those steps, you can post a MCVE along with a more specific technical question.

PS- You can preserve your code’s formatting in the forum by highlighting your code and clicking the </> code button. You can edit your post to format it now if you want.

Good luck!

I made some slight changes and have been playing around with it. I think I may have worded my question wrong, what I’m trying to do is get the what the user inputted as a solution so that it triggers and event to happen, in this case make the screen go black if the its the write answer

Which part of that is giving you trouble?

If I were you I’d start with a more basic example: can you write a simple sketch that gets a value from a user and prints it out?

Then work your way forward from there. Can you make it so the screen shows black after the user enters a specific value?

1 Like

Everything works, what I can’t figure out is how to take what the user inputs into the JOptionPane and then input it into a loop.

1 Like

I’d use the draw() function to your advantage. Remember that the draw() function runs 60 times per second. You can store your program’s state in a set of variables at the top of your sketch. Use those variables to know what to draw each frame, and then change those variables to change what’s being drawn.

Here’s a simple example:

boolean drawRed = false;

void draw(){
  if(drawRed){
    background(255, 0, 0);
  }
  else{
    background(0, 255, 0);
  }
}

void mousePressed(){
  drawRed = true;
}

The draw() method runs forever, it should include code that you want to run infinitely (e.g. drawing objects, images). Instead you’re using it like the main method. I suggest you call your methods in setup().