我试过用这个做金字塔三角形。 [英] i've tried doing a triangle of pyramid with this.
本文介绍了我试过用这个做金字塔三角形。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我这样出来。
输入数字= 5
---- *
--- ***
- *****
- *******
*********
但我的输出是......
---- *
--- **
- ***
- ****
*****
请帮帮我。如何写短代码。
谢谢..
更新!我的代码确实像这样工作。
I want my out put like this.
Input number = 5
----*
---***
--*****
-*******
*********
But my output is...
----*
---**
--***
-****
*****
please help me. how to write short code.
Thank..
Update! My code does work like this.
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
System.out.print("Input row : ");
int n = k.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = i; j > 1; j--) {
System.out.print("*");
}
//
for (int j = 1; j <= i; j++) { // my teacher don't want this line.
System.out.print("*"); // my teacher don't want this line.
}
// But teacher want my output like.
----*
---***
--*****
-*******
*********
//
System.out.println();
}
}
}
推荐答案
这不是一个重大变化从你现在的代码 - 或者它不应该是。
目前你可能正在为每一行(即一个for循环,一个输入数字)运行一个循环并输出(inputNumber - loopCounter)取消,然后loopCounter星。
相反,创建一个新变量:starsCount并从1开始。然后在你的循环内输出相同的数字下划线,但输出starsCount星,每次增加两个。
This isn't a major change from the code you have at the moment - or it shouldn't be.
At the moment you are probably running a loop for each line (i.e. a for loop, 1 to the input number) and outputting (inputNumber - loopCounter) undelines, then loopCounter stars.
So instead, create a new variable: starsCount and start it at 1. Then inside your loop output the same number of underlines, but output starsCount stars, and increase it by two each time.
这篇关于我试过用这个做金字塔三角形。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文