[Logo]
Forums Register Login
How to use (int)(Math.random()*
I've drawn two trees in Java that stand next to each other that are the same color and height. But I'm trying to code three sets of trees with different widths and get those three sets of trees to generate randomly when I run the code. The way I've coded it makes each triangle change widths at random, but I want the whole tree to be one of three sizes everytime I run and compile. What am I doing wrong?

Scanner console = new Scanner(System.in);

int width = 50* (int)(Math.random()*3+1);
int width2 = 50* (int)(Math.random()*3+1);
int width3 = 50* (int)(Math.random()*3+1);
int width4 = 50* (int)(Math.random()*3+1);
int width5 = 50* (int)(Math.random()*3+1);
int width6 = 50* (int)(Math.random()*3+1);

int height = 50;

Triangle triangle1;
Triangle triangle2;
Triangle triangle3;
Square trunk1;
Square trunk2;

Triangle triangle4;
Triangle triangle5;
Triangle triangle6;
Square trunk3;
Square trunk4;

triangle1 = new Triangle();
triangle1.changeSize(50,140);
triangle1.moveHorizontal(25);
triangle1.moveVertical(70);
triangle1.makeVisible();


triangle2 = new Triangle();
triangle2.changeSize(50,140);
triangle2.moveHorizontal(25);
triangle2.moveVertical(120);
triangle2.makeVisible();

triangle3 = new Triangle();
triangle3.changeSize(50,140);
triangle3.moveHorizontal(25);
triangle3.moveVertical(170);
triangle3.makeVisible();

trunk1 = new Square();
trunk1.changeColor("black");
trunk1.moveVertical(185);
trunk1.makeVisible();

trunk2 = new Square();
trunk2.changeColor("black");
trunk2.moveVertical(200);
trunk2.makeVisible();

triangle4 = new Triangle();
triangle4.changeSize(50,140);
triangle4.moveHorizontal(150);
triangle4.moveVertical(70);
triangle4.makeVisible();

triangle5 = new Triangle();
triangle5.changeSize(50,140);
triangle5.moveHorizontal(150);
triangle5.moveVertical(120);
triangle5.makeVisible();

triangle6 = new Triangle();
triangle6.changeSize(50,140);
triangle6.moveHorizontal(150);
triangle6.moveVertical(170);
triangle6.makeVisible();

trunk3 = new Square();
trunk3.changeColor("black");
trunk3.moveHorizontal(125);
trunk3.moveVertical(185);
trunk3.makeVisible();

trunk4 = new Square();
trunk4.changeColor("black");
trunk4.moveHorizontal(125);
trunk4.moveVertical(200);
trunk4.makeVisible();

System.out.println ("Hello, Welcome to terminal.\n");
System.out.println ("This program will draw two trees next to each other where the widths");
System.out.println ("of the triangles on the trees randomly vary between 50,100, and 150");
}
}
 

Crea Smith wrote:I want the whole tree to be one of three sizes everytime I run



I don't understand this requirement. What is the "size" of a tree?
You're not using any of the variables width, width1, width2 etc. so they can't impact anything.
The widths of the six triangles that make up the trees should be 50, 100, or 150 pixels, but the height should remain a constant 140 pixels. The size of the trunk should keep the same height and width attributes but it has to move around to accomodate the different sizes of the trees.
I posted an earlier version of my code mistakenly. Here is the latest one:

(1 like)
Does this code work or not? Please describe what exactly is the problem. Taking this code:



I've run it numerous times, and always get either 50, 100, or 150, which is what I think you want. So can you tell us exactly what is not working?
 

Crea Smith wrote:The widths of the six triangles that make up the trees should be 50, 100, or 150 pixels, but the height should remain a constant 140 pixels.



So then "int height = 50" doesn't quite agree with that. But all of this discussion seems kind of futile when we don't know what that code is doing. Is it producing some kind of picture?
You might find it easier to import java.util.Random, create a Random object, and call nextInt(3) on that Random object.
Crea Smith, welcome to the Ranch.

Please UseCodeTags when you post source code - that way your code will be much easier to read on the forum.
 

Jesper de Jong wrote:Crea Smith, welcome to the Ranch.

Please UseCodeTags . . .

Sorry, I hadn't notice he was a new member. In that case I'll add some code tags
Your mind is under my control .... your will is now mine .... read this tiny ad
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature


This thread has been viewed 2336 times.

All times above are in ranch (not your local) time.
The current ranch time is
Apr 21, 2018 15:24:47.