2017-03-31 83 views
0

我已经在这个作业问题上工作了大约4天,现在我快要疯了。我们对此代码有特定的指示;填满二维数组正方形并以特定图案打印

“你的程序需要进行如下操作:

  1. 显示欢迎信息

  2. 提示用户输入一个整数,它是3.如果输入的号码是< 3.保持。提示用户,直到他们输入数字3(使用do/while)。这个数字将决定正方形数组的大小。

  3. 按照模式1填充数组d使用printf显示它以格式化数组。

  4. 按照模式2填充相同的数组,并使用printf t0格式显示数组。

  5. 显示关闭消息“

模式:。enter image description here

我仍然停留在模式的一个我第一次尝试做for循环做这在它有一个if语句,用于检查列号是否为偶数,如果是,则向后打印代码。问题还建议使用while循环和do/while循环...?

还有关于如何去关于第二种模式的任何提示。

这是我的代码。

import java.util.Scanner; 
public class a3q33 
{ 

    public static void main(String[] args) 
    { 
    Scanner keyboard = new Scanner(System.in); 

    int n; 

    do 
    { 
     System.out.println("How many rows/columns do you want your array to have? (Must be at least 3)"); 

     n=keyboard.nextInt(); 

    } while(n < 3); 

    int [][] arr = new int [n][n]; 

    int i, j, k=1; 

    for(i=0;i<n;i++) 
    { 
     if(i % 2 != 0) 
     { 
      for(j=n;j>0;j--) 
      { 
       k = k+n; 
       arr[i][j]=k; 
       k--; 
      } 
     } 

     else 
     { 
      for(j=0;j<n;j++) 
      { 
       arr[i][j]=k; 
       k++; 
      } 
     } 
    } 

    for(i=0;i<n;i++) 
    { 
     for(j=0;j<n;j++) 
     { 
      System.out.printf(arr[i][j]+" "); 

     } 
      System.out.printf(""); 
      System.out.println(); 
    } 

    } 
} 

任何帮助将不胜感激!

回答

0

我认为你的意思是“行”而不是“列”的条件检查,因为约定是arr [row] [column]?

此外,for循环代码:

for(j=n;j>0;j--) 

这将减小行/列指数下降到1,而不是0。所以你会错过改编一个元素[0] [... ]或者arr [0]。 另外,j = n将超出界限。

相反,尝试使用:

for(j=n-1;j>-1;j--) 

这将是寻找到一个很好的第一步。

0
import java.util.Scanner; 

public class a3q33{ 
    public static void main(String[] args){ 
     Scanner keyboard = new Scanner(System.in); 
     int n; 
     do{ 
      System.out.println("How many rows/columns do you want your array to have? (Must be at least 3)"); 
      n=keyboard.nextInt(); 
     } while(n < 3); 

     int count = 1; 
     int [][] arr = new int [n][n]; 
     for(int i = 0;i<n; i++){ 
      if(i%2==0){ 
       for(int j = 0;j<n; j++){ 
        arr [i][j] = count; 
        count++; 
       } 
      } 
      else{ 
       for(int j = n-1;j>=0; j--){ 
        arr [i][j] = count; 
        count++; 
       } 
      }    
     } 


     System.out.println("\nPattern 1 \n"); 
     // print the array 
     for (int i = 0; i < n; i++){ 
      for (int j = 0; j < n; j++){ 
       System.out.printf("%d\t",arr[i][j]); 
      } 
      System.out.println(); 
     } 
     // reset count back to 1 and fill the array in the same way only without the if/else 
     // for n = 5 for example this will produce [1, 2, 3, 4, 5 ] 
     //           [6, 7, 8, 9, 10] 
     //           [11,12, 13, 14, 15] 
     //           [16,17, 18, 19, 20] 
     //           [21,22, 23, 24, 25] 

     count = 1; 
     for(int i = 0;i<n; i++){    
      for(int j = 0;j<n; j++){ 
       arr [i][j] = count; 
       count++; 
      } 
     } 

     // rotate arrays using arraycopy; for array at index 1 rotate by one, at index 2 by 2 and so on see 
     //http://stackoverflow.com/questions/31174840/how-to-rotate-an-array 
     // 
     for (int i = 1; i<n; i++){ 
      int [] temp = new int [n]; 
      for (int j = 0; j < n; j++) { 
       temp[(j + i) % n] = arr[i][j]; 
      } 
      System.arraycopy(temp, 0, arr[i], 0, n); 
      arr[i]=temp; 
     } 

     System.out.println("\nPattern 2 \n"); 
     // print the array 
     for (int i = 0; i < n; i++){ 
      for (int j = 0; j < n; j++){ 
       System.out.printf("%d\t",arr[i][j]); 
      } 
      System.out.println(); 
     } 
    } 
} 
0

您可以通过检查模实现这一点:

for (int i = 0; i < n; i++) { 
    int start = i * n; 
    for (int j = 0; j < n; j++) { 
     arr[i][j] = i * n + ((i % 2 === 0) ? (j + 1) : (n - j)); 
    } 
}