Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于输入查找多边形数字的Java代码_Java_Eclipse_Numbers - Fatal编程技术网

基于输入查找多边形数字的Java代码

基于输入查找多边形数字的Java代码,java,eclipse,numbers,Java,Eclipse,Numbers,我需要一个可以执行以下示例的程序: “多边形的边数是多少?”“3” “你想看其中多少?”“36” “1 3 6 10 15 21 28 36 45 55” “66 78 91 105 120 136 153 171 190 210” “496 528 561 595 630 666” -它必须能够输入前两个问题的值,然后打印答案,每行10个。我自己也试过写代码,但总是陷入for循环。谢谢 这就是我到目前为止所做的: Scanner input = new Scanner(System.in

我需要一个可以执行以下示例的程序:

“多边形的边数是多少?”“3”

“你想看其中多少?”“36”

“1 3 6 10 15 21 28 36 45 55”

“66 78 91 105 120 136 153 171 190 210”

“496 528 561 595 630 666”

-它必须能够输入前两个问题的值,然后打印答案,每行10个。我自己也试过写代码,但总是陷入for循环。谢谢

这就是我到目前为止所做的:

   Scanner input = new Scanner(System.in);

  //Request number of sides the polygon must have
  System.out.println("What is the number of sides of your polygon? ");
  n = input.nextInt();

  System.out.println("How many of these would you like to see? ");
  k = input.nextInt();

  long output = polygonalNumber(n,k);
   for (k = 1; k < k; k++);
     if (output % 10 == 0) {
         System.out.println();
     }

 }
     public static long polygonalNumber(long n, long k){

 long p = (k * k) * (n-2) - (k * (n-4))/2;

 return polygonalNumber(n,k);
扫描仪输入=新扫描仪(System.in);
//请求多边形必须具有的边数
System.out.println(“多边形的边数是多少?”);
n=input.nextInt();
System.out.println(“您希望看到多少?”;
k=input.nextInt();
长输出=多边形编号(n,k);
对于(k=1;k
您的代码看起来真的很糟糕。但不用担心,我编写了一个支持3-6个方面的版本

这是:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // Request number of sides the polygon must have
    System.out.println("What is the number of sides of your polygon? ");
    int sides = input.nextInt();

    System.out.println("How many polygonal numbers do you want?");

    int amount = input.nextInt();
    input.nextLine();
    input.close();

    FindPolygonalNumbers(sides, amount);
}

static void FindPolygonalNumbers(int sides, int amount) {
    String numbers = "";
    for (int i = 1; i <= amount; i++) {
        int number = 0;
        if (sides == 3) {
            number = (i * (i + 1)) / 2;
        } else if (sides == 4) {
            number = i * i;
        } else if (sides == 5) {
            number = (3 * i * i - i) / 2;
        } else if (sides == 6) {
            number = i * (2 * i - 1);
        }
        numbers = numbers + number + ", ";

    }
    System.out.println(numbers);
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
//请求多边形必须具有的边数
System.out.println(“多边形的边数是多少?”);
int sides=input.nextInt();
System.out.println(“您想要多少个多边形数字?”);
int amount=input.nextInt();
input.nextLine();
input.close();
FindPolygonalNumber(边数、数量);
}
静态无效FindPolygonalNumber(整数边,整数量){
字符串编号=”;

对于(int i=1;我请清楚地询问您的问题,并输入一些您迄今为止尝试过的代码,以便其他人可以尝试帮助您。