Java:使用数组变量的方法

时间:2015-05-25 22:48:04

标签: java

所以我在网上使用各种片段制作了这段代码,以便我可以看到它是如何工作的。但是由于一些奇怪的原因,第4个“For”循环被完全跳过,而且我不确定为什么。任何帮助,将不胜感激。它是一个使用代码的命令行。

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(" ");
    }
}

编辑:第4个“For”循环是

                    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;
                    }

2 个答案:

答案 0 :(得分:1)

循环的第四个是:

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(" ");
    }

正如您所看到的,它位于名为printArray()的方法中。阵列没有任何问题。这很好。问题是永远不会调用该方法,因此for循环永远不会运行。

这是java methods tutorial

答案 1 :(得分:0)

//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] + " ");
}

有三种基本方法可以打印数组中的所有元素。建议:你应该避免使用静态方法,这在你的情况下是错误的。

New1 task = new New1();
task.doSomething();