本文介绍了如何使用Java打印带框架的钻石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我仍然是一名学习者,我面临着编写 Java 代码的问题,该代码打印带有包含钻石的框架的钻石.我曾尝试制作镜框,但在制作钻石时遇到了挑战.
Hello I am still a learner, I am faced with writing a java code that prints a diamond with a frame enclosing the diamond. I have tried making the frame, but I am having challenge forming the diamond.
以下是钻石外观示例:
这是我的代码示例:
System.out.print("\n");
// for the top cover
System.out.print("+");
for (int i = 0; i <= (size * 2); i++) {
System.out.print("-");
}
System.out.println("+");
// for the side
int count = 0;
for (int i = 1; i <= (size * 2) - 1; i++) {
System.out.print("|");
for (int j = 0; j <= (size * 2); j++) {
System.out.print(" ");
}
System.out.println("|");
}
// For the bottom
System.out.print("+");
for (int i = 0; i <= (size * 2); i++) {
System.out.print("-");
}
System.out.println("+");
推荐答案
这里是:
public static void main(String[] args) {
drawDiamond(6);
}
private static void drawDiamond(int size) {
System.out.print("\n");
// for the top cover
System.out.print("+");
for (int i = 0; i < size * 2 - 1; i++) {
System.out.print("-");
}
System.out.println("+");
//first half
for (int i = 1; i < size; i++) {
System.out.print("|");
for (int j = 0; j < size - i; j++) {
System.out.print("-");
}
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print("*");
}
for (int j = 0; j < size - i; j++) {
System.out.print("-");
}
System.out.println("|");
}
//middle line
System.out.print("|");
for (int i = 0; i < size * 2 - 1; i++) {
System.out.print("*");
}
System.out.println("|");
//second half
for (int i = 1; i < size; i++) {
System.out.print("|");
for (int j = 0; j <= (i * 2 - 1) / 2; j++) {
System.out.print("-");
}
for (int k = 0; k < (size - i) * 2 - 1; k++) {
System.out.print("*");
}
for (int j = 0; j <= (i * 2 - 1) / 2; j++) {
System.out.print("-");
}
System.out.println("|");
}
// For the bottom
System.out.print("+");
for (int i = 0; i < size * 2 - ((size + 1) % 2); i++) {
System.out.print("-");
}
System.out.println("+");
}
输出:
+-----------+
|-----*-----|
|----***----|
|---*****---|
|--*******--|
|-*********-|
|***********|
|-*********-|
|--*******--|
|---*****---|
|----***----|
|-----*-----|
+-----------+
有什么问题吗?
这篇关于如何使用Java打印带框架的钻石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!