在java中创建数字模式 [英] Creating number patterns in java

查看:97
本文介绍了在java中创建数字模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据用户输入的许多行创建这些模式:

  1 
12
123
1234
12345

54321
4321
321
21
1


1
21
321
4321
54321

我可以做前两个,但我不能做第三个。

这是第二个代码:



<$ p $ (int i = 1; i< = lines; i ++){
for(int j =行+ 1 - i; j> 0; j--)
System.out.print(j +);
System.out.println();
}
}


解决方案

<$ p $ (int i = 1; i <= lines; i ++){
System.out.print(int i = 1; i< = lines; i ++){

$ b ();
for(int j = lines; j> 0; j--)
System.out.print(j> i?:j);
System.out.println();
}
}


I need to create these patterns in java according to however many lines the user enters:

1
12
123
1234
12345

54321
4321
321
21
1


     1
    21
   321
  4321
 54321

I can do the first two but I cannot do the third.
Here is the code for the second:

public static void displayPatternII (int lines) {

    for (int i = 1; i <= lines; i++){
        for (int j = lines + 1 - i; j > 0; j--)
            System.out.print (j + " ");
        System.out.println();
    }
}

解决方案

 public static void displayPatternIII (int lines) {

    for (int i = 1; i <= lines; i++){
      System.out.print(" ");
        for (int j = lines; j > 0; j--)
            System.out.print (j > i ? " " : j);
        System.out.println();
    }
}

这篇关于在java中创建数字模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆