How to use "if" for randomized lines of an array of string?

edited October 2013 in Questions about Code

Following code writes 5 random lines; each line writes either "Hello Earth!" or "Hello Mars!"

I want to draw a blue circle if more "Hello Earth!" is written than "Hello Mars!" and a red circle if else.

I wrote the following code but I don't know what to write between paranthesis for if ( ), whatever I tried I got an error (e.g. I cannot write "planets[0] > 2"). I would appreciate if you can help me on this. As you can understand, I am a beginner.

String[] planets = new String[2]; 
planets[0] = "Hello Earth!"; 
planets[1] = "Hello Mars!"; 

for (int i = 0; i < 5; i++)
{
  int planet = int(random(planets.length));
  println(planets[planet]);
  int diameter = 100; 
size(200, 200);
background(0);
if( **???** ) {
  noStroke();
  fill(255, 0, 0); 
  ellipse(100, 100, diameter, diameter);
}
else{
  noStroke();
  fill(20, 20, 255);
  ellipse(100, 100, diameter, diameter);
  }
}

Answers

  • Answer ✓

    You probably need to keep the count of how many times "Hello Earth!"s and "Hello Mars!"s were displayed.

    Since you are using an array for the Strings, maybe you need another array of int, each item initialized as 0 at the beginning.

    Then increase the right element by 1 each time you print a string.

    In the if() you can then write something like if(counter[0] > counter[1]) (in case your variable is called counter).

    Cheers!

  • Can you or anyone show me how to do that? I'm not sure how to write a code to counter random lines. Thank you in advance.

  • Answer ✓
    int pCount[] = new int[2];
    String[] planets = new String[2];
    planets[0] = "Hello Earth!";
    planets[1] = "Hello Mars!";
    
    for (int i = 0; i < 5; i++)
    {
      int planet = int(random(planets.length));
      println(planets[planet]);
      pCount[planet]++;
      int diameter = 100;
    size(200, 200);
    background(0);
    if( pCount[1] > pCount[0] ) {
      noStroke();
      fill(255, 0, 0);
      ellipse(100, 100, diameter, diameter);
    }
    else{
      noStroke();
      fill(20, 20, 255);
      ellipse(100, 100, diameter, diameter);
      }
    }
    
  • Thank you very much.

  • edited October 2013

    Some improvements to the program above... 1. You should not set the size() of your screen more than once. In that code it's called 5 times. 2. The planet is drawn also 5 times. I think it would make more sense to draw the planet outside of the loop, once you are sure of which planet you want to draw. 3. To avoid repeating lines of code, the calls to noStroke() and ellipse() can be placed after the if() statement.

    int diameter = 100;
    int pCount[] = new int[2];
    String[] planets = new String[2];
    planets[0] = "Hello Earth!";
    planets[1] = "Hello Mars!";
    
    size(200, 200);
    
    for (int i = 0; i < 5; i++) {
      int planet = int(random(planets.length));
      println(planets[planet]);
      pCount[planet]++;
    }
    if ( pCount[1] > pCount[0] ) {
      fill(255, 0, 0);
    } else {
      fill(20, 20, 255);
    }
    background(0);
    noStroke();
    ellipse(100, 100, diameter, diameter);
    
Sign In or Register to comment.