简体   繁体   English

Pascal Triangle在Java中无法正确打印?

[英]Pascal Triangle Not Printing Correctly in Java?

I got an assignment that requires us to print out pascal's triangles based on the user entered value of N. We were provided a main that allows the user to calculate Pascal's Triangle based on a value of n. 我得到一个作业,要求我们根据用户输入的N值打印出Pascal的三角形。我们提供了一个主控件,允许用户根据n的值计算Pascal的Triangle。 In this case if n is 0, then Pascal's Triangle is 1. Otherwise for n being greater than 0, the appropriate Pascal's Triangle will be created and displayed. 在这种情况下,如果n为0,则Pascal的三角为1。否则,如果n大于0,则将创建并显示适当的Pascal的三角。 Here is the main: 这是主要的:

public class CSCD210Lab13
{
   public static void main(String[] args)
   {
      int n = 0;
      int [][] pascal = null;


      do
      {
         n = Lab13Methods.readN();
         pascal = Lab13Methods.createPascalsTriangle(n);
         Lab13Methods.printPascals(pascal);

      }while(MyUtil.goAgain());

   }// end main

}// end class

Here is my Methods file: 这是我的方法文件:

import java.util.*;

public class Lab13Methods
{
   public static int readN()
   {
      Scanner kb = new Scanner(System.in);
      System.out.println("Enter N: ");
      int n = kb.nextInt();
      while(n < 0)
      {
         System.out.println("Number Below 1. Re-Enter: ");
         n = kb.nextInt();
      }
      return n;
   }


   public static int[][] createPascalsTriangle(int n)
   {
      int[][]pascalTri = new int[n + 1][(n + 1) * 2];

      int sideOne, side;

      pascalTri[0][n - 1] = 1;
      sideOne = side = n - 1;

      for (int y = 1; y < n; y++)
      {
          pascalTri[y][sideOne] = 1;
          pascalTri[y][side] = 1;
          sideOne--;
          side++;

          for (int k = 1; k <= y; k++)
          {
              int left = pascalTri[y - 1][sideOne + (2 * k) - 1];
              int right = pascalTri[y - 1][sideOne + (2 * k) + 1];
              pascalTri[y][sideOne + (2 * k)] = left + right;
          }
      }
      return pascalTri;
  }

  public static void printPascals(int[][]pascal)
  {
      for (int f = 0; f < pascal.length; f++)
      {
          for (int v = 0; v < pascal[f].length; v++)
          {
              if (pascal[f][v] == 0)
              {
                  System.out.print("");
              } 
              else
              {
                  System.out.print(pascal[f][v]+" ");
              }
          }
          System.out.println();
      }
  }
}

Here is my goAgain file: 这是我的goAgain文件:

public static boolean goAgain()
{
  boolean goAgain = false;
  String answer;
  Scanner kb = new Scanner(System.in);
      System.out.println();
      System.out.print("Do you want to go again? ");
      answer = kb.nextLine();

      while(!answer.equalsIgnoreCase("yes") && !answer.equalsIgnoreCase("no"))
      {
        System.out.print("Invalid Input. Do you want to go again? ");
        answer = kb.nextLine();
      }
      if(answer.equalsIgnoreCase("yes"))
      { 
        goAgain = true; 
      }
      else if(answer.equalsIgnoreCase("no"))
      {
        goAgain = false;
      }
      return goAgain;
   }      
}

My question is about how it's printing. 我的问题是关于它如何打印。 If I enter 10 to be the value of N, this is how it is supposed to print: 如果我输入10作为N的值,这就是应该打印的方式:

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
1 10 45 120 210 252 210 120 45 10 1 

However, this is how mine prints: 但是,这是我的打印方式:

1 
1 1 1 1 
1 1 2 1 1 
1 1 3 3 1 1 
1 1 4 6 4 1 1 
1 1 5 10 10 5 1 1 
1 1 6 15 20 15 6 1 1 
1 1 7 21 35 35 21 7 1 1 
1 1 8 28 56 70 56 28 8 1 1 

What am I doing wrong? 我究竟做错了什么?

Refer this short pascal code i have written which is depend on user input: 请参阅我编写的此简短的Pascal代码,具体取决于用户输入:

public class Pascal {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scanner= new Scanner(System.in);
    System.out.println("Enter the Number of levels of Pascal");
    int levelCount = scanner.nextInt();

    for(int i =0;i<levelCount;i++) {
        int value = 1;
        for(int j=0;j<=i;j++) {
            System.out.println(value);
             value = value * (i - j) / (j + 1);

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


}

} }

Enjoy..!! 请享用..!!

I don't know if you know what a pascal triangle is let me explain to you what it is. 我不知道您是否知道帕斯卡三角形是什么,让我向您解释它是什么。

11^0 = 1
11^1 = 11
11^2 = 121
11^3 = 1331
11^4 = 14641
11^5 = 161051

I don't know why have you done some much code all when you need was 我不知道为什么在需要的时候就做了很多代码

public static void printPascalsTriangle(int n)
{
    long number=11l;
    for(int i=0;i<=n;i++)
    {
        System.out.println(new Double(Math.pow(number,i)).longValue());
    }
}

You would need a case more that five which can be handled like this link . 您需要的案例超过五个,可以像此链接一样处理。

I think your error may be here: 我认为您的错误可能在这里:

      pascalTri[y][sideOne] = 1;
      pascalTri[y][side] = 1;
      sideOne--;
      side++;

Your program is designed to fill in the cells of the array in a checkerboard pattern: for any two adjacent rows, one row will have non-zero entries only in even-numbered locations, and the other will have non-zero entries only in odd-numbered locations. 您的程序设计为以棋盘格模式填充数组的单元格:对于任何两个相邻行,一行仅在偶数位置具有非零条目,而另一行仅在奇数位置具有非零条目编号的位置。

Notice that right after you do pascalTri[y][sideOne] = 1; 请注意,对你以后pascalTri[y][sideOne] = 1; , you decrement sideOne . ,您递减sideOne That means if you are in a row that should be using odd-numbered cells, sideOne now is odd, but when you did pascalTri[y][sideOne] = 1; 这意味着如果您所在的行应该使用奇数单元格,则sideOne现在是奇数,但是当您执行pascalTri[y][sideOne] = 1; , sideOne was still even. sideOne甚至都没有。 So you have put an even-numbered entry in a row that should have only odd-numbered entries. 因此,您将偶数编号的条目放置在一行中,该行应仅具有奇数编号的条目。 That is where all the extra 1s are coming from in your output. 这就是所有额外的1都来自输出的地方。

Just delete these lines: 只需删除以下行:

      pascalTri[y][sideOne] = 1;
      pascalTri[y][side] = 1;

All they are doing is creating those extra, unwanted 1 values. 他们所做的只是创建那些多余的多余1值。 All the correct values are being written in the array by other statements. 其他所有语句将所有正确的值写入数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM