Reputation: 13
I am attempting to have my program print(command window) this> "----------" in any length from 1 - 100:
public void display()
{
for (int x=0; x < Length; x++)
{
System.out.print("-");
}
}
However, I need a "bug" to land somewhere on this wire: for example: ----0---- and be able to remember that position and move along the wire. I am not asking for the answer to solve this but help on what keywords to lookup and reading on something like this.
Thanks for the help!
Upvotes: 0
Views: 115
Reputation: 9658
However, I need a "bug" to land somewhere on this wire
You can use Random
as follows:
public static void main (String[] args) throws Exception
{
Random rnd = new Random();
int randomNumber = rnd.nextInt(100);
char[] chr = new char[100];
Arrays.fill(chr,'-');
chr[randomNumber] = '0';
System.out.print(new String(chr));
}
Here, randomNumber
will hold the position of the bug for you.
Upvotes: 1
Reputation: 555
One way of doing that is by calling method with argument that specifies position of the bug. That way you know position of the bug.
public void display(int position)
{
for (int x=0; x < Length; x++)
{
if (x == position) {
System.out.print("O");
}
else {
System.out.print("-");
}
}
}
Upvotes: 1