2015-05-25 67 views
1

所以我做了使用網絡上的各種片段這段代碼,這樣我可以看到方法它是如何工作的,但由於一些奇怪的原因,第四屆「for」循環被完全忽略,而且我不確定爲什麼。任何幫助,將不勝感激。它是一個使用代碼的命令行。的Java:使用數組變量

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

編輯:第四屆 「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; 
        } 
+0

我假設第4個循環在方法printArray()中。那是對的嗎? – Forseth11

+1

第4個for循環是從未調用過的方法。也許你應該從一些教程開始? https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html – Radiodef

+0

4th for循環實際上是使用Click()方法的,我只使用printArray()來檢出數組看看他們是否工作正常 –

回答

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(); 
1

第四for循環:

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