我在完成此计划时遇到困难。我正在尝试制作一个程序来创建星号,然后将其变成三角形。
这是我已经拥有的。
public class 12345 {
public static void main(String[] args) {
int n = 0;
int spaces = n;
int ast;
System.out.println("Please enter a number from 1 - 50 and I will draw a triangle with these *");
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
for (int i = 0; i < n; i++) {
ast = 2 * i + 1;
for (int j = 1; j <= spaces + ast; j++) {
if (j <= spaces)
System.out.print(' ');
else
System.out.print('*');
}
System.out.println();
spaces--;
}
}
}
它正在创建星号,但是我如何能够在它们形成三角形的地方继续它们......所以它们随着移动而变大,然后变小......
提前谢谢您!
最佳答案
尝试移动
int spaces = n;
从标准输入读取 n
的值之后。
这解决了您一半的问题,并希望让您走上正轨。
关于java - Java 初学者 - 控制输出 - 生成 Asterix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9188952/