Reputation: 23
So I made this code using various snippets on the web so that I could see how it all works, But For some strange reason the 4th "For" loop is skipped entirely, And I'm not sure why. Any help would be appreciated. It is a command line using code.
public class New1
{
public static void main(String[] args) throws InterruptedException
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of clicks before repeat: ");
int Clicks = in.nextInt();
int rep2 = 0;
int Waits[] = new int[Clicks];
Clicks = Clicks * 2;
int Coords[] = new int[Clicks];
Clicks = Clicks / 2;
int Gung;
int Ho;
int Yo;
int xco = 0;
int yco = 1;
if(Clicks > 0)
{
for (int rep = 0; rep < Coords.length; rep++)
{
System.out.print("Enter x coord: ");
Coords[rep] = in.nextInt();
rep++;
System.out.println(" ");
System.out.print("Enter y coord: ");
Coords[rep] = in.nextInt();
System.out.println(" ");
System.out.print("Enter the pause (In seconds) between this click and the next click: ");
Waits[rep2] = in.nextInt();
rep2++;
System.out.println(" ");
}
rep2 = 0;
for (int rep3 = 0; rep3 < Waits.length; rep3++)
{
Waits[rep3] = Waits[rep3] * 1000;
}
System.out.print("How many times to repeat click sequence? : ");
int Revolutions = in.nextInt();
for (int counter = 0; counter > Revolutions; counter++)
{
for (int Flicks = 0; Flicks > Clicks; Flicks++)
{
Gung = Coords[xco];
Ho = Coords[yco];
Yo = Waits[Flicks];
Click(Gung, Ho);
Thread.sleep(Yo);
xco += 2;
yco += 2;
}
xco = 0;
yco = 1;
}
}
}
public static void Click(int x, int y)
{
Robot bot = null;
try
{
bot = new Robot();
}
catch (Exception failed)
{
System.err.println("Failed instantiating Robot: " + failed);
}
int mask = InputEvent.BUTTON1_DOWN_MASK;
bot.mouseMove(x, y);
bot.mousePress(mask);
bot.mouseRelease(mask);
}
public static void printArray(int arr[])
{
int n = arr.length;
for (int ar = 0; ar < n; ar++)
{
System.out.print(arr[ar] + " ");
}
System.out.println(" ");
}
}
Edit: The 4th "For" loop is
for (int Flicks = 0; Flicks > Clicks; Flicks++)
{
Gung = Coords[xco];
Ho = Coords[yco];
Yo = Waits[Flicks];
Click(Gung, Ho);
Thread.sleep(Yo);
xco += 2;
yco += 2;
}
Upvotes: 1
Views: 64
Reputation: 49656
//first way
System.out.println(Arrays.toString(arr));
//second way
for(int i : arr) {
System.out.print(i + " ");
}
//third way
for(int i = 0; i < arr.length; ++i) {
System.out.print(arr[i] + " ");
}
There three basic ways to print all elements in array. Advice: you should avoid using static methods, it is wrong in your case.
New1 task = new New1();
task.doSomething();
Upvotes: 0
Reputation: 1438
The fourth for loop is:
public static void printArray(int arr[])
{
int n = arr.length;
for (int ar = 0; ar < n; ar++)
{
System.out.print(arr[ar] + " ");
}
System.out.println(" ");
}
As you can see it is inside a method called printArray(). There is nothing wrong with the array. It is just fine. The problem is that the method is never called thus the for loop never runs.
Here is a java methods tutorial.
Upvotes: 1