java输出一个空心菱形
时间: 2023-11-20 08:05:56 浏览: 65
以下是 Java 代码,可以输出一个空心菱形:
```java
import java.util.Scanner;
public class HollowDiamond {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入菱形的行数:");
int n = scanner.nextInt();
int mid = (n + 1) / 2;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (Math.abs(j - mid) + Math.abs(i - mid) == mid - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
运行程序后,输入菱形的行数,即可输出一个空心菱形。
相关问题
java 输出一个空心菱形
好的,请稍等,我来为您生成一个空心菱形:
```
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入菱形最长对角线的长度(奇数):");
int len = scanner.nextInt();
for(int i=1;i<=len;i++){
for(int j=1;j<=len-i;j++){
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++){
if(j==1||j==i*2-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
for(int i=len-1;i>=1;i--){
for(int j=1;j<=len-i;j++){
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++){
if(j==1||j==i*2-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
请将以上代码复制到您的编辑器中,并编译运行即可生成空心菱形。希望对您有所帮助。
Java输出一个空心菱形
以下是Java代码,可以输出一个空心菱形:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入菱形的大小:");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
输出示例:
```
请输入菱形的大小:5
*
* *
* *
* *
* *
* *
* *
* *
*
```
阅读全文