在Java中创建数字模式(续) [英] creating number patterns in java(continued)
问题描述
我需要根据用户输入的行数在Java中创建这些模式:
I need to create these patterns in java according to however many lines the user enters:
1
12
123
1234
12345
1
12
123
1234
12345
54321
4321
321
21
1
54321
4321
321
21
1
12345
1234
123
12
1
12345
1234
123
12
1
我可以做前两个,但不能做第三个。每个数字之间也需要一个空格。
I can do the first two but I cannot do the third. I also need a space between each number. Please help!
这是我第一次使用的代码:
Here is the code I used for the first:
public static void displayPatternI(整数行){
public static void displayPatternI (int lines) {
for (int i = 1; i <= lines; i++){
for (int j = 1; j <= i; j++)
System.out.print (j + " " );
System.out.println();
}
}
编辑:
这是代码第二种模式:
this is the code for the second pattern:
for(int i = 1; i <= lines; i ++){
for(int j = lines + 1-i; j> 0; j--)
System.out.print(j +);
System.out.println();
for (int i = 1; i <= lines; i++){ for (int j = lines + 1 - i; j > 0; j--) System.out.print (j + " "); System.out.println();
我尝试了广泛修改第一个代码,但是我无法获得第三个模式...
I have tried modifying the first code extensively but I cannot get the third pattern...
推荐答案
只需将您的第一个for循环更改为减量而不是增量
Simply change your first for loop to decrement instead of increment
for (int i = lines; i > 0; i--) {
而不是
for (int i = 1; i <= lines; i++){
这篇关于在Java中创建数字模式(续)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!