用Java实现菱形的打印输出

  1. import java.util.Scanner;
  2. public class dengyao2 {
  3. public dengyao2() {
  4. super();
  5. }
  6. public static void main(String[] args) {
  7. System.out.println("请输入菱形的边长:");
  8. Scanner input = new Scanner(System.in);
  9. if (input.hasNextInt()) {
  10. int a = input.nextInt();
  11. dengyao2 triangle = new dengyao2();
  12. triangle.function(a);
  13. } else {
  14. System.out.println("你输入的格式有误!请输入整数!!");
  15. }
  16. input.close();
  17. }
  18. public void function(int x) {
  19. for (int i = 1; i <= x; i++) {
  20. for (int j = 1; j <= x - i; j++) {
  21. System.out.print(" ");
  22. }
  23. for (int k = 1; k <= 2 * i - 1; k++) {
  24. System.out.print("*");
  25. }
  26. System.out.println();
  27. }
  28. for (int i = x - 1; i > 0; i--) {
  29. for (int j = 1; j <= x - i; j++) {
  30. System.out.print(" ");
  31. }
  32. for (int k = 1; k <= 2 * i - 1; k++) {
  33. System.out.print("*");
  34. }
  35. System.out.println();
  36. }
  37. }
  38. }
上一篇:CentOS6.5下的Nagios安装配置详解(图文)


下一篇:jQuery源代码阅读之二——jQuery静态属性和方法