Java 如何打印具有用户指定行数的整数三角形?
我想进一步练习ArrayList和嵌套循环的使用,所以我想到了以下有趣的问题 行数由用户指定,结果如下:Java 如何打印具有用户指定行数的整数三角形?,java,arraylist,nested-loops,Java,Arraylist,Nested Loops,我想进一步练习ArrayList和嵌套循环的使用,所以我想到了以下有趣的问题 行数由用户指定,结果如下: Enter a number: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 有人能给我一些关于做这个问题的建议吗?提前谢谢 我的第一个forloop用于创建三角形行,第二个用于创建列 对于每一行,需要先打印值,然后打印空格 每行空间数应减少一个,每列空间数应增加一个
Enter a number: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
有人能给我一些关于做这个问题的建议吗?提前谢谢 我的第一个forloop用于创建三角形行,第二个用于创建列 对于每一行,需要先打印值,然后打印空格 每行空间数应减少一个,每列空间数应增加一个 对于居中输出,每行增加两颗星星
import java.util.Scanner;
class TriangleExample
{
public static void main(String args[])
{
int rows, number = 1, counter, j;
//To get the user's input
Scanner input = new Scanner(System.in);
//take the no of rows wanted in triangle
System.out.println("Enter the number of rows for triangle:");
//Copying user input into an integer variable named rows
rows = input.nextInt();
System.out.println(" triangle");
System.out.println("****************");
//start a loog counting from the first row and the loop will go till the entered row no.
for ( counter = 1 ; counter <= rows ; counter++ )
{
//second loop will increment the coloumn
for ( j = 1 ; j <= counter ; j++ )
{
System.out.print(number+" ");
//Incrementing the number value
number++;
}
//For new line
System.out.println();
}
}
}
import java.util.Scanner;
类三角形示例
{
公共静态void main(字符串参数[])
{
int行,数字=1,计数器,j;
//获取用户的输入
扫描仪输入=新扫描仪(System.in);
//取三角形中需要的行数
System.out.println(“输入三角形的行数:”);
//将用户输入复制到名为rows的整数变量中
rows=input.nextInt();
System.out.println(“三角形”);
System.out.println(“*******************”);
//从第一行开始龙计数,循环将一直进行到输入的行号。
对于(counter=1;counter最好的学习方法是通过实践。尝试一些东西,然后在这里发布你的问题。:)它清晰而准确!但是变量“number”与变量“i”和“j”没有关系在for循环中。它为什么工作?number变量初始化为1,然后在每次解析时递增1,因此它在三角形中打印1,2,3…10