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

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

Trying to add using a multiple in a for statement

问题

  1. 我正试图将 x 变成一个倍数金字塔例如如果倍数是 2那么下方的数字会以 2 递增我尝试过程中一直得到混乱并且交换的倍数想知道是否有人可以友善地帮助
  2. 以下是我目前的代码
  3. import java.util.Scanner;
  4. class Main
  5. {
  6. public static void main(String[] args)
  7. {
  8. int rows=0;
  9. int multiple=0;
  10. int x=0;
  11. Scanner scan = new Scanner(System.in);
  12. System.out.println("输入要生成的行数:");
  13. rows = scan.nextInt();
  14. System.out.println("输入要递增的倍数:");
  15. multiple = scan.nextInt();
  16. System.out.println();
  17. for (int i = 1; i<=rows; i++)
  18. {
  19. for (int j = 1; j<=i; j++)
  20. {
  21. System.out.print(x);
  22. x += multiple;
  23. }
  24. System.out.println();
  25. }
  26. }
  27. }
  28. 例如
  29. 输入行数
  30. 5
  31. 输入递增的倍数
  32. 1
  33. 0
  34. 1 2
  35. 3 4 5
  36. 6 7 8 9
  37. 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:

  1. import java.util.Scanner;
  2. class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. int rows=0;
  7. int multiple=0;
  8. int x=0;
  9. Scanner scan = new Scanner(System.in);
  10. System.out.println(&quot;Input Rows to Generate: &quot;);
  11. rows = scan.nextInt();
  12. System.out.println(&quot;Input Multiple to Count by: &quot;);
  13. multiple = scan.nextInt();
  14. System.out.println();
  15. for (int i = 1; i&lt;=rows; i++)
  16. {
  17. for (int j = 1; j&lt;=i; j++)
  18. {
  19. System.out.print(&quot;x&quot;);
  20. }
  21. System.out.println();
  22. }
  23. }
  24. }

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

  1. import java.util.Scanner;
  2. public class Main
  3. {
  4. public static void main(String[] args) {
  5. int rows=0;
  6. int multiple=0;
  7. int x=0;
  8. Scanner scan = new Scanner(System.in);
  9. System.out.println("输入要生成的行数:");
  10. rows = scan.nextInt();
  11. System.out.println("输入要递增的倍数:");
  12. multiple = scan.nextInt();
  13. System.out.println();
  14. int n = 0;
  15. for (int i = 1; i <= rows; i++){
  16. for (int j = 1; j <= i; j++){
  17. System.out.print(multiple * n + " ");
  18. n++;
  19. }
  20. System.out.println();
  21. }
  22. }
  23. }

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

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

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

  1. 0
  2. 2 4
  3. 6 8 10
  4. 12 14 16 18
  5. 20 22 24 26 28
英文:
  1. import java.util.Scanner;
  2. public class Main
  3. {
  4. public static void main(String[] args) {
  5. int rows=0;
  6. int multiple=0;
  7. int x=0;
  8. Scanner scan = new Scanner(System.in);
  9. System.out.println(&quot;Input Rows to Generate: &quot;);
  10. rows = scan.nextInt();
  11. System.out.println(&quot;Input Multiple to Count by: &quot;);
  12. multiple = scan.nextInt();
  13. System.out.println();
  14. int n = 0;
  15. for (int i = 1; i &lt;= rows; i++){
  16. for (int j = 1; j &lt;= i; j++){
  17. System.out.print(multiple * n + &quot; &quot;);
  18. n++;
  19. }
  20. System.out.println();
  21. }
  22. }
  23. }

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

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

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

  1. 0
  2. 2 4
  3. 6 8 10
  4. 12 14 16 18
  5. 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:

确定