java星号直角三角形 [英] java asterisk right triangle

查看:647
本文介绍了java星号直角三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试一个直角三角形星号。但我只得到这个
输出:(我不能在这里放星号)

I'd like to try a right triangle asterisk. But I only got this output: (I can't place here the asterisk)

@ 
@@
@@@

我想要的是这个:

  @
 @@
@@@

这是我编程的代码:

 public class triangles {
public static void main( String[] args ) {

    for( int i = 1; i <= 10; i++ ){
        for( int j = 0; j < i; j++ ){
            System.out.print("*");

        }
        System.out.println();
    }

}
  }

任何想法分享非常感谢。我是一个java新手。谢谢。

Any ideas to share are greatly appreciated. I'm a java newbie. thanks.

推荐答案

您的代码永远不会打印任何空格,这应该是个问题。

Your code never prints any spaces, that should be a problem.

你可以使用这个简单的方法:

You can use this simple approach:

for (int i = 0; i < 3; i++) System.out.println("  @@@".substring(i, i+3));

逻辑非常简单:你有两个空格和三个符号的字符串。第一行输出需要是两个空格和一个符号,因此这是字符串的前三个字符。第二行应该是一个空格和两个符号—这是跳过第一行后的三个字符串;依此类推:你只是滑过字符串,每次从头开始跳过另外三个字符。

The logic is quite simple: you have the string with two spaces and three at-signs. The first line of output needs to be two spaces and a single at-sign, so that's the first three chars of the string. The second line should be one space and two at-signs—that's the three chars of the string after skipping the first one; and so on: you just slide through the string, each time skipping one more from the beginning and taking the next three chars.

这篇关于java星号直角三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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