Pascal的三角形 - 寻找第n行

时间:2016-11-14 02:30:31

标签: java

我的代码有问题。它必须要求用户输入第n行,然后打印该行。错误在第62行,当我删除箭头时,它没有给出建议。

使用此: enter image description here

import java.util.Scanner;

public class PascalsTriangle {
  private int lineNumber, count;
  private int[] num;

  public PascalsTriangle() {
    lineNumber = 1;
  }

  public PascalsTriangle(int n) {
    set(n);
  }

  public void set(int n) {
    if (n < 1)
      lineNumber = n;
  }

  public int get() {
    return lineNumber;
  }

  private void pascal(int[] row) {
    if (count >= lineNumber)
      return;
    num = new int [row.length+1];
    num[0] = 1;

    for (int i = 1; i < row.length; i++)
      num[i] = row[i-1] + row[i];
    num[row.length] = 1;
    count++;
    pascal(num);
    return;
  }

  public int[] output() {
    count = 1;
    num = new int[count];
    return num;
  }

  public static void main(String[] args) {
    int i, num;

    Scanner scan = new Scanner (System.in);
    System.out.println("Enter the Nth number: ");

    num = scan.nextInt();
    PascalsTriangle t = new PascalsTriangle(num);
    int[] result = t.output();
    System.out.println("Line" + t.get());
    for (i = 0; < result.length; i++)
      System.out.println(result[i] + " ");
  }
}

1 个答案:

答案 0 :(得分:1)

第62行的for循环应为:

for(i=0; i < result.length ; i++){

逻辑错误留给你,因为这似乎是一项家庭作业。