✨✨个人主页:沫洺的主页
📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏
📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏
📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏
💖💖如果文章对你有所帮助请留下三连✨✨
通过StringUtils工具类实现
package test1;
import org.apache.commons.lang3.StringUtils;
public class Rhombus {
public static void main(String[] args) {
for (int i = 1; i <= 21; i += 4)
System.out.println(StringUtils.center("*".repeat(i), 21));
for (int i = 17; i >= 1; i -= 4)
System.out.println(StringUtils.center("*".repeat(i), 21));
}
}
运行结果如下
*
*****
*********
*************
*****************
*********************
*****************
*************
*********
*****
*
Process finished with exit code 0
这里导入了个jar包(commons-lang3)
链接:https://pan.baidu.com/s/1NgM50O0X71Qm6LncPyReiw?pwd=wznb
提取码:wznb
下载下来
操作如下(将jar包导入所需项目中)
这样就可以了
具体IDEA创建模块可以参考:
有关IDEA创建模块的内容都在里面
附带for循环实现的空心菱形
public class HollowRhombus {
public static void main(String[] args) {
int l = 11;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < l; j++) {
System.out.print(" ");
}
l-=2;
for (int j = 0; j <= i*4+1; j++) {
if(j==1||j==i*4+1)System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
l=3;
for (int i = 5; i > 0; i--) {
for (int j = 0; j < l; j++) {
System.out.print(" ");
}
l+=2;
for (int j = 0; j <= (i-1)*4+1; j++) {
if(j==1||j==(i-1)*4+1)System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
}
}
运行结果如下
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Process finished with exit code 0