我有这个代码,但它正在打印这样的对角线...我想从右上角到左下角,任何想法如何转动它?
*
*
*
*
*
代码:
class Diagonal {
public static void main(String args[]) {
int row, col;
String spaces = " ";
for( row = 1; row < 6; row++) {
System.out.println(spaces +"*");
spaces += " ";
}
}
}
答案 0 :(得分:2)
通过为每个附加行插入一个空格来构建对角线。因此,如果从一些行开始并删除空格,则应该进行反转。但是我们需要清理我们如何做空间,这样我们就可以更容易地减去每行的数量。
class Diagonal{
public static void main(String args[]) {
int row, col;
for( row = 6; row > 0; row--) {
for (int x = 0; x < row; x++) {
System.out.print(" ");
}
System.out.print("*\n");//note carriage return
}
}
}