英文:
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("Input Rows to Generate: ");
rows = scan.nextInt();
System.out.println("Input Multiple to Count by: ");
multiple = scan.nextInt();
System.out.println();
for (int i = 1; i<=rows; i++)
{
for (int j = 1; j<=i; j++)
{
System.out.print("x");
}
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 = 5
和 multiple = 1
,输出为:
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
同样,对于 rows = 5
和 multiple = 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("Input Rows to Generate: ");
rows = scan.nextInt();
System.out.println("Input Multiple to Count by: ");
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();
}
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论