如何用for循环创建完美的急剧三角形? [英] How Can I Make Perfect Acute Triangle with for loop?

查看:133
本文介绍了如何用for循环创建完美的急剧三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  0 
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 $
6 6 6 6 6 6 6
7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9

但是当我想要超过10的时候,我的锐角三角形会变成这样:

  0 
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6 6
7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10 10


12 12 12 12 12 12 12 12 12 12 12 12

我的代码如下

  public class Triangel {

/ **
* @param args命令行参数
* /
public static void main(String [] args){

for(int i = 20;我> 0; i - ){
for(int j = i - 1; j> 0; j - ){
System.out.print(); (i =< 9){
System.out.print(

(int k = 20; k> i - 1; (+(20-i));
} else if(i> 9){
System.out.print(+(20-i));
}
}
System.out.println();





$ b $ p
$ b $它完美的急性三角形,当我想9后打印?



注意:我认为if语句会帮助,但它似乎没有。 $ b

解决方案

改变Inner for循环 k = i-1 i> 9 i <9 ,所以11将被排除。我想你正试图管理两位数字和一位数字之间的空格。但是你可以使用单一的 i <= 10 和双倍空间for i> 10

  for(int k = 20; k> ; i  -  1; k  - ){
if(i <= 10){
System.out.print(+(20 - i)); //单个空间$ b $如果(i> 10){
System.out.print(+(20-i)); // Double Space
}
}

注意:这个三角形可能看起来有些怪异

Just for playing I tried to write a program so I can make a acute triangle with using a for loop, so my out put will like this:

                 0
                1  1
               2  2  2
              3  3  3  3
             4  4  4  4  4
            5  5  5  5  5  5
           6  6  6  6  6  6  6
          7  7  7  7  7  7  7  7
         8  8  8  8  8  8  8  8  8
        9  9  9  9  9  9  9  9  9  9

but when I want to go more than 10 my acute triangle gets messed up like this:

                 0
                1  1
               2  2  2
              3  3  3  3
             4  4  4  4  4
            5  5  5  5  5  5
           6  6  6  6  6  6  6
          7  7  7  7  7  7  7  7
         8  8  8  8  8  8  8  8  8
        9  9  9  9  9  9  9  9  9  9
       10  10  10  10  10  10  10  10  10  10  10


12           12           12           12           12           12           12           12           12           12           12           12           12

My code is as follows

public class Triangel {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        for (int i = 20; i > 0; i--) {
            for (int j = i - 1; j > 0; j--) {
                System.out.print(" ");
            }
            for (int k = 20; k > i - 1; k--) {
                if (i =< 9) {
                    System.out.print("           " + (20 - i));
                } else if (i > 9) {
                    System.out.print( "  "+(20 - i));
                }
            }
            System.out.println();
        }
    }
}

Any idea how I can make it perfect acute triangle when I wanna print after 9?

Note: I thought that the if statement would help, but it seems it does not.

解决方案

Just Change Inner for loop : here for k=i-1 as you are checking for i>9 and i<9 so 11 will be excluded. I guess you are trying to manage space between two digit and one digit number. But you can just use single for i<=10 and double space for i>10

       for (int k = 20; k > i - 1; k--) {
            if (i <= 10) {
                System.out.print(" "+(20 - i));//Single Space
            } else if (i > 10) {
                System.out.print("  "+(20 - i));//Double Space
            }
        }

NOTE:This triangle may look weird little bit

这篇关于如何用for循环创建完美的急剧三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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