大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
1.任意菱形
//菱形
Scanner myscanner = new Scanner(System.in);
int n = myscanner.nextInt();
for (int i = 0; i < n / 2 + 1; i++) {//控制行数
for (int j = 0; j < n / 2 - i; j++) {//打印空格
System.out.print(" ");
}
for (int k = 0; k < 2 * i +1; k++) {//打印*
System.out.print("*");
}
System.out.println("\n");
}
for (int i = 1; i < n / 2 + 1; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int k = 2 * (n / 2 - i) + 1; k > 0; k--) {
System.out.print("*");
}
System.out.println("\n");
}
2.空心菱形
方法1
int n;
Scanner myscanner = new Scanner(System.in);
n = myscanner.nextInt();
for (int i = 0; i < n / 2 + 1; i++) {//控制行数
for (int j = 0; j < n / 2 + 1 - i; j++) {
System.out.printf(" ");
}
for (int k = 0; k < 2 * i + 1; k++) {
if (k == 0 || k == 2 * i){//输出第一颗和最后一颗*
System.out.printf("*");
}else System.out.printf(" ");
}
System.out.printf("\n");//控制换行
}
for (int i = n / 2; i >= 0 ; i--) {
for (int j = n / 2 - i + 1; j > 0 ; j--) {
System.out.printf(" ");
}
for (int k = 0; k < 2 * i + 1; k++) {
if (k == 0 || k == 2 * i){//输出第一颗和最后一颗*
System.out.printf("*");
}else System.out.printf(" ");
}
System.out.printf("\n");
}
方法2
Scanner myscanner = new Scanner(System.in);
int n = myscanner.nextInt();
for (int i = -n; i <= n; i++) {
for (int j = -n; j <= n; j++) {
int k = Math.abs(i) + Math.abs(j);
if (k == n){
System.out.printf("*");
}else {
System.out.printf(" ");
}
}
System.out.println("\n");
}
3.边框菱形
Scanner myscanner = new Scanner(System.in);
int n = myscanner.nextInt();
for (int i = -n; i <= n; i++) {
for (int j = -n; j <= n; j++) {
int k = Math.abs(i) + Math.abs(j);
if (k >= n){
System.out.printf("*");
}else {
System.out.printf(" ");
}
}
System.out.println("\n");
}
4.数字菱形
Scanner myscanner = new Scanner(System.in);
int n = myscanner.nextInt();
for (int i = 0; i < n; i++) {
int k = 1;
int j;
for (j = 0; j < Math.abs(n / 2 -i); j++) {
System.out.printf(" ");
}
for ( j = 0; j < n - 2 * Math.abs(n / 2 - i); j++) {
System.out.print(k);
if (j < n / 2 - Math.abs(n / 2 -i)){
k++;
}else {
k--;
}
}
System.out.println("\t");
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/187790.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...