2013-11-22 47 views
0

这是它应该打印的模式。这是我现在所做的代码。每个数字之间应有5个空格。有没有人可以帮助我呢?如何以三角形模式打印数字

模式:

   1 
      2  0  2 
    3  0  0  0  3 

我的代码:

import java.util.Scanner; 

public class Triangle{ 

public static void main(String args[]) { 

    System.out.println("Input the number of lines you want to print."); 
    Scanner a = new Scanner(System.in); 
    int n = a.nextInt(); 
    int[] row = new int[0]; 
    for(int i=0 ; i < n ; i++){ 
     row = nextRow(row); 
      for(int j=0;j < n-i;j++){ 

      //Padding For Triangle 
      System.out.print(" "); 

     } 
     //Output the values 
     for(int j=0 ; j < row.length ; j++){ 

      System.out.print(row[j]+" "); 

     } 
     //Start New Line 
     System.out.println(); 
    } 
} 
/*Find Values Of Next Row*/ 
public static int[] nextRow(int row[]){ 

    int nextRow[] = new int [row.length+1]; 

    nextRow[0] = row.length+1;    
    nextRow[nextRow.length-1] =row.length+1; 
    for(int i=1 ; i < nextRow.length-1 ; i++){ 

     nextRow[i] = 0; 
    } 
    return nextRow; 
} 
} 
+0

所以......有什么问题吗? – Augusto

+0

这还不够清楚。请重新制定 –

+0

目前的产量是多少? –

回答

-2

查找关于打印输出零的等差数列,剩下的应该是微不足道的。

+2

当你投票给别人投票时,请留下评论,这样我们可以在将来改进......没有意义的只有投票,然后逃跑,它没有改变。 – fYre

-1

我看到你的代码,我认为我们能做到这一点,请按照我修改后的代码:

public class Triangle { 

public static void main(String args[]) { 

    System.out.println("Input the number of lines you want to print."); 
    Scanner a = new Scanner(System.in); 
    int n = a.nextInt(); 
    int [] row = new int[0]; 
    for (int i = 0; i < n; i++) { 
     row = nextRow(row); 
     for (int j = 0; j < n - i; j++) { 

      // Padding For Triangle 
      System.out.print(" "); 

     } 
     // Output the values 
     for (int j = 0; j < row.length; j++) { 

      System.out.print(row[j] + "  "); 

     } 
     // Start New Line 
     System.out.println(); 
    } 
} 

/* set space between each other. */ 
public static String printSpace(int n) { 
    String result = ""; 
    for (int i = 0; i < n; i++) { 
     result += "  ";// 5 space 
    } 

    return result; 
} 

/* Find Values Of Next Row */ 
public static int [] nextRow(int row[]) { 

    int nextRow[] = new int[row.length + 1]; 

    nextRow[0] = row.length + 1; 
    nextRow[nextRow.length - 1] = row.length + 1; 
    for (int i = 1; i < nextRow.length - 1; i++) { 

     nextRow[i] = 0; 
    } 
    return nextRow; 
} 

}


可能这是回答你想要什么。

0

这是一些修改后的代码。它不完整,仍然需要更新您的代码才能获得所需的结果。这一部分我将为你留下。我还在代码中留下了提示,以便您可以修改。

1.

 // Padding For Triangle 
    System.out.print("  "); // 6 white space 

2.

 System.out.print(row[k]); 
    System.out.print("  "); // 5 white space 

3.

/* Find Values Of Next Row */ 
    @SuppressWarnings("null") 
    public static int[] nextRow(int row[]) { 
     int nextRow[] = null; 
     if (row.length == 0) { 
      nextRow = new int[1]; 
      nextRow[0] = 1; 
     } else { 
      // count++; // Hint 
      nextRow = new int[row.length + 2]; 

      for (int i = 0; i < nextRow.length; i++) { 
       if ((i == 0 || i == nextRow.length - 1)) { 

        nextRow[i] = nextRow.length - 2; 
        // nextRow[i] = count; 

       } else { 
        nextRow[i] = 0; 

       } 
      } 
     } 
     return nextRow; 
    } 

如果您有任何问题,只是问。祝你好运。

0

你首先要确定的是,输出预计的格局,而你的情况是:

Row0, columns 1 
Row1, columns 3 
Row2, columns 5 
Row3, columns 7 

是,的rowNum * 2 + 1

基础的,在此,我修改您的代码这里是工作的解决方案:

import java.util.Scanner; 

public class Triangle{ 
public static void main(String args[]) { 
    System.out.println("Input the number of lines you want to print."); 
    Scanner a = new Scanner(System.in); 
    int n = a.nextInt(); 
    int[] row = null; 
    for(int i=0 ; i < n ; i++){ 
     row = nextRow(i); 
     for(int j=0;j < n-i;j++){ 
      //Padding For Triangle 
      System.out.print(" "); 
     } 
     //Output the values 
     for(int j=0 ; j < row.length ; j++){ 
      System.out.print(row[j]+" "); 
     } 
     //Start New Line 
     System.out.println(); 
    } 
} 
/*Find Values Of Next Row*/ 
public static int[] nextRow(int rowNum){ 
    int nextRow[] = new int [rowNum*2+1]; 

    nextRow[0] = rowNum+1;//-rowNum/2; 
    nextRow[nextRow.length-1] = nextRow[0]; 
    for(int i=1 ; i < nextRow.length-1 ; i++){ 
     nextRow[i] = 0; 
    } 
    return nextRow; 
} 
} 

,这里是一些输出:

2:

1 
    2 0 2 

5:

  1 
     2 0 2 
     3 0 0 0 3 
    4 0 0 0 0 0 4 
    5 0 0 0 0 0 0 0 5 

10:

    1 
       2 0 2 
       3 0 0 0 3 
      4 0 0 0 0 0 4 
      5 0 0 0 0 0 0 0 5 
     6 0 0 0 0 0 0 0 0 0 6 
     7 0 0 0 0 0 0 0 0 0 0 0 7 
    8 0 0 0 0 0 0 0 0 0 0 0 0 0 8 
    9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 
10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10