尝试在for循环中使用乘法添加。

huangapple go评论71阅读模式
英文:

Trying to add using a multiple in a for statement

问题

我正试图将 x 变成一个倍数金字塔例如如果倍数是 2那么下方的数字会以 2 递增我尝试过程中一直得到混乱并且交换的倍数想知道是否有人可以友善地帮助
以下是我目前的代码

import java.util.Scanner;

class Main 
{
  public static void main(String[] args) 
  {
   int rows=0;
   int multiple=0;
   int x=0;

   Scanner scan = new Scanner(System.in);
   System.out.println("输入要生成的行数:");
   rows = scan.nextInt();
   System.out.println("输入要递增的倍数:");
   multiple = scan.nextInt();
   System.out.println();

   for (int i = 1; i<=rows; i++)
   {
    for (int j = 1; j<=i; j++)
    {
      System.out.print(x);
      x += multiple;
    }
    System.out.println();
   } 
  }
}

例如
输入行数
5
输入递增的倍数
1

0         
1         2         
3         4         5         
6         7         8         9         
10        11        12        13        14
英文:

I'm trying to change the x into a pyramid of multiples. Ex. if the multiple was 2 it would add by 2s going down. I kept getting multiples that would mess up and swap around when I tried and was wondering if anyone would be kind enough to help!
This is what I have so far:

import java.util.Scanner;

class Main 
{
  public static void main(String[] args) 
  {
   int rows=0;
   int multiple=0;
   int x=0;

   Scanner scan = new Scanner(System.in);
   System.out.println(&quot;Input Rows to Generate: &quot;);
   rows = scan.nextInt();
   System.out.println(&quot;Input Multiple to Count by: &quot;);
   multiple = scan.nextInt();
   System.out.println();

   for (int i = 1; i&lt;=rows; i++)
   {
    for (int j = 1; j&lt;=i; j++)
    {
      System.out.print(&quot;x&quot;);
    }
    System.out.println();
   } 
  }
}

ex.
Enter the number of rows:

5

Enter the multiple to count by:

1

0

1 2

3 4 5

6 7 8 9

10 11 12 13 14

答案1

得分: 1

import java.util.Scanner;

public class Main 
{
  public static void main(String[] args) {
    int rows=0;
    int multiple=0;
    int x=0;

    Scanner scan = new Scanner(System.in);
    System.out.println("输入要生成的行数:");
    rows = scan.nextInt();
    System.out.println("输入要递增的倍数:");
    multiple = scan.nextInt();
    System.out.println();
    int n = 0;
    for (int i = 1; i <= rows; i++){
      for (int j = 1; j <= i; j++){
        System.out.print(multiple * n + " ");
        n++;
      }
      System.out.println();
    } 
   
  } 
}

对于 rows = 5multiple = 1,输出为:

0 
1 2 
3 4 5 
6 7 8 9 
10 11 12 13 14 

同样,对于 rows = 5multiple = 2,输出为:

0 
2 4 
6 8 10 
12 14 16 18 
20 22 24 26 28 
英文:
import java.util.Scanner;

public class Main 
{
  public static void main(String[] args) {
  int rows=0;
  int multiple=0;
  int x=0;

  Scanner scan = new Scanner(System.in);
  System.out.println(&quot;Input Rows to Generate: &quot;);
  rows = scan.nextInt();
  System.out.println(&quot;Input Multiple to Count by: &quot;);
  multiple = scan.nextInt();
  System.out.println();
  int n = 0;
  for (int i = 1; i &lt;= rows; i++){
    for (int j = 1; j &lt;= i; j++){
      System.out.print(multiple * n + &quot; &quot;);
      n++;
    }
    System.out.println();
  } 

 } 
}

For rows = 5 and multiple = 1 the output will be:

0 
1 2 
3 4 5 
6 7 8 9 
10 11 12 13 14 

Also, for rows = 5 and multiple = 2 the output will be:

0 
2 4 
6 8 10 
12 14 16 18 
20 22 24 26 28 

huangapple
  • 本文由 发表于 2020年10月26日 01:43:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64526850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定