dimanche 13 novembre 2016

Using IF statements

In this program, the user will be asked to input 2 words, (firstWord and secondWord). The two words will automatically appear on the screen once the object is created.

firstWord is set to change it's position on the screen via a while loop.

secondWord will stay in a fixed position.

I want the loop to end once the X position or Y position of firstWord is the same as secondWord.

The XY position of secondWord = (155, 361)

So for instance, something like:

if (word1.xPosition = 155) {
    break;
}

Obviously that code is wrong, it is just to give you an idea of what I want to achieve. So how would I write this if statement?

My code is pasted below:

public words(String w1, String w2)
{
    // initialise instance variables
    firstWord = new Text(w1);
    firstWord.randomizePosition();
    firstWord.changeColor("green");
    firstWord.changeSize(48);
    firstWord.makeVisible();
    secondWord = new Text(w2);
    secondWord.setPosition(155, 361);
    secondWord.changeColor("orange");
    secondWord.changeSize(48);
    secondWord.makeVisible();



public void draw()
{
   while(true) {

    firstWord.randomizePosition();

}    
    if(firstWord.xPosition =  ){

    break;    

}

}

Replace the if statement

Aucun commentaire:

Enregistrer un commentaire