流程控制
化繁为简,先死后活
一道题领会循环:
打印空三角,如图所示:
import java.util.*;
public class Stars {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input floor:");
int floor = scanner.nextInt();
scanner.close();
for (int i = 1; i <= floor; i++) {
for(int j = 1; j <= floor - i; j++){
System.out.print(" ");
}
for(int k = 1; k <= 2 * i - 1; k++){
if(k == 1 || k == 2 * i -1 || i == floor){
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
评论