Java 实例 – 打印菱形
Java 教程
· 2019-02-10 18:43:09
输出指定行数的菱形。
实例
public class Diamond {
public static void main(String[] args) {
print(8); // 输出 8 行的菱形
}
public static void print(int size) {
if (size % 2 == 0) {
size++; // 计算菱形大小
}
for (int i = 0; i < size / 2 + 1; i++) {
for (int j = size / 2 + 1; j > i + 1; j--) {
System.out.print(" "); // 输出左上角位置的空白
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*"); // 输出菱形上半部边缘
}
System.out.println(); // 换行
}
for (int i = size / 2 + 1; i < size; i++) {
for (int j = 0; j < i - size / 2; j++) {
System.out.print(" "); // 输出菱形左下角空白
}
for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
System.out.print("*"); // 输出菱形下半部边缘
}
System.out.println(); // 换行
}
}
}
输出结果:
* *** ***** ******* ********* ******* ***** *** *
点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html
The Java Language Specification, Java SE 7 Edition
James Gosling、Bill Joy、Guy L. Steele Jr.、Gilad Bracha、Alex Buckley / Addison-Wesley Professional / 2013-2-24 / USD 59.99
Written by the inventors of the technology, The Java(r) Language Specification, Java SE 7 Edition, is the definitive technical reference for the Java programming language. The book provides complete, ......一起来看看 《The Java Language Specification, Java SE 7 Edition》 这本书的介绍吧!