2015-05-08 43 views
1

我的程序的目标是让2d数组打印从1到80.我试图让它看起来有点像这样。如何让二维数组打印从1到80

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 26 27 28 29 30 
31 32 33 34 35 36 37 38 39 40 
41 42 43 44 45 46 47 48 49 50 
51 52 53 54 55 56 57 58 59 60 
61 62 63 64 65 66 67 68 69 70 
71 72 73 74 75 76 77 78 79 80 

以后我可以修复格式化和一切,但我如何得到的二维数组,从1-80打印的数字?我附上了迄今为止完成的代码。

公共类转换 {

public static void main (String[] args) 
    { 

     int[][] twoarray = new int[8][10]; 

     int i ; 
     int j ; 

     for(i =0; i < 8; i++) 
     { 
      for(j = 0; j < 10; j++) 
      twoarray[i][j] = (i * j); 
     } 


     for(i = 0; i < 8; i++) 
     { 
      for(j = 0; j < 10; j++) 
      { 

       System.out.print(i); 
       System.out.print(" "); 
       System.out.print(j); 
       System.out.print(" "); 


      } 
      System.out.println(); 
     } 
    } 
} 
+3

你的代码甚至不使用填充数组。 –

+1

@JohnJack,如果你得到了解决你的问题的答案,请[标记为已接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 – aioobe

回答

0

更多的澄清

for(i =0; i < 8; i++) 
    { 
     for(j = 0; j < 10; j++) 
     twoarray[i][j] = (i * 10+j+ 1); 
    } 
    /* when i=0, j=0, value is 0*10+0+1=1 -->1st Row 
     when i=0, j=1, value is 0*10+1+1=2 --> 1st Row 
     when i=7,j=0, value is 7*10+0+1=71 --> 8th Row 
     when i=7, j=9, value is 7*10+9+1=80 --> 8th Row */ 

更新:如何使用1- d阵列

public static void main(String arg[]) 
{ 
    int array[]=new int[80]; 

    //filling array upto 80 
    for(int i=0;i<=79;i++) 
     array[i]=i+1; 

    //display array on console 
    int counter=0; 
    for(int i=0;i<=79;i++) 
    { 
     counter++; 
     System.out.print(array[i]+" "); 

     if(counter==10) 
     { 
      /*printing new line 
      after counting upto 10 elements*/ 
      System.out.println(); 

      //reseting counter to 0; 
      counter=0; 
     } 
    } 
} 

输出 然而Fi来做到这一点第一行可能不与下面的行对齐,因为第一行中的元素是一位数,所以空间有小故障,这可以很容易解决,但我不认为这会对您造成问题,因为您只需要打印8行和10列。

enter image description here

+0

星期六斯里阿卡尔。你知道我可以如何将这个二维数组转换成一维数组,并仍然得到相同的输出?非常感谢你的帮助。 –

+0

sat sri akal ji!你当然可以。这将比这更容易,将更新我的答案。 –

4

你初始化数组错误

twoarray[i][j] = (i * j); 

应该

twoarray[i][j] = (i * 10 + j + 1); 

作为一个方面说明,这非常类似于如何您计算屏幕上像素的内存地址(内存偏移量)给定它的X和Y坐标以及屏幕宽度。

然后实际使用二维数组

for(i = 0; i < 8; i++) 
{ 
    for(j = 0; j < 10; j++) 
    { 
     System.out.print(twoarray[i][j]); 
    } 
} 
+1

'twoarray [i] [j] =(i * 10 + j + 1);' – Samurai

+0

好的......我习惯于计算基于0的内存偏移:-) –

0

我从这个代码得到的输出是一样的,你想你的问题是什么。

请注意,输出控制台有这种奇怪的格式,其中第一行不与其他行对齐。如果您将其输出到文本区域组件,它应该看起来很好。你的问题是你忘了计算数组的实际值。相反,您试图将该值与您的计数器相乘。这个计划中的柜台就是那个柜台。希望这个作品为你:)

public class Conversion { 

public static void main(String[] args) { 
    int[][] twoArray = new int[8][10]; 

    int i; // i & j are just counters to iterate through your array 
    int j; 
    int sum = 0; // This will keep track of the sum/ counting from 1 to 80 

    for(i = 0; i < 8; i++){ 
     for (j = 0; j<10; j++){ 
      sum+= 1; // Increment with its own value + 1. Therefor it is counting. 
      twoArray[i][j] = (sum); 
     } 
    } 

    for(i = 0; i < 8; i++){ 
     for(j=0; j< 10;j++){ 
       System.out.print(twoArray[i][j]); //Unload the array. 
       System.out.print(" "); 
     } 
     System.out.println(); 
    } 
} 

}

这里是一维数组版本:

只使用这个,如果你真的真的真的需要的数组,因为它是相当多余的,因为你可以直接打印到控制台(检查注释掉的代码)。

public class Conversion { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    int[] numbers = new int[80]; 

    int count = 0; 

    for (int K = 0; K<80; K++) { 
     count+= 1; 
     numbers[K] = count; 
    } 

    count = -1;//reinitialize count. 
    for (int L = 0; L<8; L++){ 
     System.out.println(""); 
     for (int M = 0; M<10; M++){ 

      count+= 1; 

      if(count == 80){ 
       return; 
      }else { 
       System.out.print(numbers[count] + " "); 
      } 
     } 
    } 


    /* int count = 0; //reinitialize count. You can get the same results by just using the code here 
              //if you really need the array then use the un-commented code. 
    for (int L = 0; L<8; L++){ 
     System.out.println(""); 
     for (int M = 0; M<10; M++){ 
      count+= 1; 
      System.out.print(" " + count); 
     } 
    }*/ 
} 

}

+0

另外你需要使用你的数组实际产出。 –

+0

你知道我怎么可以用1d数组来做到这一点。我的一个朋友告诉我,我可以将这个二维数组转换成一维数组,并且仍然有相同的输出?你知道怎么做吗?十分感谢你的帮助。 –

+0

@John Jack hey如果您找到了适合您的答案,请将其标记为已接受。 :) –

0

您可以使用一个二维数组,甚至一维数组做到这一点。这是代码。

public class Test{ 
    public static void main(String args[]){ 
     int r = 8, c = 10; //You can change this 
     int[][] toArray = new int[r][c]; //using 2D array 
     for(int i = 0; i < r; i++){ //i is the ten's place 
      for(int j = 0; j < c; j++){ //j is the one's place 
       toArray[i][j] = i*10 + j + 1; 
       System.out.print(toArray[i][j]+"\t"); 
      } 
      System.out.println(); 
     } 

     System.out.println("\n\n\n"); 

     int a[] = new int[r*c]; //using 1D array 
     for(int i = 0; i < r*c; i++){ 
      a[i] = (i+1); //+1 since we're starting with 1 
      System.out.print(a[i]+"\t"); 
      if(a[i]%10==0) //Go to the next line if number is 10, 20, 30,.. 
       System.out.println(); 
     } 
    } 
} 

当然,在这个程序中不需要数组,您可以直接打印数值。