java 打印 空心菱形
2016-09-23 17:36
686 查看
在JavaScript课上,老师要求我们打印空心菱形,有位同学的思路很独特,我简单修改了一下,在这里分享出来。
package important; public class PrintHollowDiamond { public static String p(int x, int y, int center) { //当x、y轴大于对称轴时,对cneter取模 if (x > center) // x = 2 * center - x; x = x % center; if (y > center) // y = 2 * center - y; y = y % center; //如果x+y等于对称轴,则返回"*" if (x + y == center) return "*"; return "-"; } public static void main(String[] args) { int line = 3; int n = 2 * line - 1; //构建一个n行*n行的坐标系 int center = line - 1; //x轴和y轴的对称轴 /** * center在这里为2,也就是菱形关于x=2和y=2对称 * * 0 1 2 3 4 x轴 * - - - - - * 0| * * 1| * * * 2|* * * 3| * * * 4| * * y * 轴 */ for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { System.out.print(p(x, y, center)); } System.out.println(); } } }
相关文章推荐
- Java多重循环算法问题:(九九乘法表、打印平行四边形、打印空心菱形、打印菱形、搬砖问题、斐波那契数列、猴子吃桃问题、回文数、1!+2!+...+20!)
- Java控制台打印一个空心菱形
- Java打印空心菱形,非常简单的操作
- [Java基础]打印空心菱形
- 用java打印空心菱形
- 如何使用JAVA打印菱形(四)对于空心菱形外加对角线的美化
- java中打印实心菱形以及空心菱形的方法
- 用Java打印空心菱形
- java中打印实心菱形以及空心菱形的方法
- java打印菱形,空心菱形
- Java小程序:打印空心菱形
- java 实现打印输出一个空心菱形
- Java 打印空心菱形
- java 循环问题 大全 求2~100a之内的素数。求水仙花数:(1~1000)。有1、2、3、4四个数字,输出所有能组成的互不相同且无重复的三位数。打印空心菱形
- 用java实现打印菱形,空心菱形
- 如何使用JAVA打印菱形(三)空心菱形基础上加上对角线
- 用java打印空心菱形
- Java用 * 打印图形-菱形、空心菱形、三角形等
- java打印实心菱形,空心菱形,for循环嵌套
- java学习之打印菱形和空心菱形