简体   繁体   English

查找五边形数字并以10乘10的方式打印

[英]Finding pentagonal numbers and printing them 10 by 10

I have a code to find the pentagonal numbers and print them 10 by 10. However I cannot figure out how to print the pentagonal numbers out other than the final value. 我有一个代码来查找五角形数字并以10乘10的形式打印它们。但是,除了最终值,我无法弄清楚如何打印出五角形数字。

public class Lab_6_1{
   public static void main (String[] args){


  System.out.println("Pentagonal Numbers are: ");
  getPentagonalNumber(100);
   }

   public static int getPentagonalNumber(int n){


  int pentNum=((n*(3*n-1))/2);
  final int numPerLine=10;

  for(n=1;n<=100;n++)
  {
  System.out.print(" "+n);
  if (n % 10==0)
  System.out.print("\n");
  }
  System.out.print(pentNum);
  return pentNum;


   }
}

I don't want blatant answers. 我不要公然的答案。 Just advice and/or hints to figure it out. 只是建议和/或提示找出来。

Your whole logic isn't doing what is supposed to do. 您的整个逻辑没有执行应做的事情。 I have made following changes and it works as expected: 我进行了以下更改,并且按预期工作:

public static void main(String[] args) {

    System.out.println("Pentagonal Numbers are: ");
    getPentagonalNumber(100);
}

public static void getPentagonalNumber(int n) {


    final int numPerLine = 10;
    int pentNum;

    for (int i = 1; i <= n; i++) {
        pentNum = ((i * (3 * i - 1)) / 2);
        System.out.print(" " + pentNum);
        if (i % numPerLine == 0)
            System.out.print("\n");
    }

}

As you can see I have made some changes in for loop and calculating the pentNum for every time the loop runs and prints it. 如您所见,我在for循环中进行了一些更改,并在每次循环运行并打印时计算pentNum

Hope this helps 希望这可以帮助

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

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