[英]Display an ASCII diamond with nested for loops
我正在尝试使用嵌套的for
循环显示星号菱形。
到目前为止,这是我的代码:
public class Diamond {
public static void main(String[] args) {
int size = 9;
for (int i = 1; i <= size; i += 2) {
for (int k = size; k >= i; k -= 2) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}// end loop
for (int i = 1; i <= size; i += 2) {
for (int k = 1; k <= i; k += 2) {
System.out.print(" ");
}
for (int j = size; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}// end loop
}
}
这很接近,但我打印了 9 个星号的行两次。
如何调整第二个for
循环以在 7 个星号和 2 个空格处开始输出?
在你的第一个 for 循环中删除=
标记并使用<
例如
for (int i = 1; i < size; i += 2)
完整代码:
int size = 9;
for (int i = 1; i < size; i += 2) {
for (int k = size; k >= i; k -= 2) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}// end loop
for (int i = 1; i <= size; i += 2) {
for (int k = 1; k <= i; k += 2) {
System.out.print(" ");
}
for (int j = size; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}// end loop
输出:
*
***
*****
*******
*********
*******
*****
***
*
通过使用作为 Java-11 一部分引入的String#repeat
,您可以使用单个循环来完成。
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
输出:
*
***
*****
*******
*********
*******
*****
***
*
通过将空间量增加一个字符,您还可以打印钻石的变体:
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "* ".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
输出:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
int n = 9;
for (int i = 0; i < n; i++) {
for (int k = n - 1; k > i; k--) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int j = 0; j < n - 1; j++) {
for (int k = j; k >= 0; k--) {
System.out.print(" ");
}
for (int i = 2 * (n - j - 1) - 1; i > 0; i--) {
System.out.print("*");
}
System.out.println("");
}
输出:
*
***
*****
*******
*********
***********
*************
***************
*****************
***************
*************
***********
*********
*******
*****
***
*
只是为了好玩... :) 试试我的代码....
public class Diamond {
static String sp(int n) {
String s = "";
for (int i = 0; i < n; i++)
s += " ";
return s;
}
static String st(int n) {
String s = "";
for (int i = 0; i < n; i++)
s += "*";
return s;
}
static int abs(int n) {
if (n < 0)
return -n;
else
return n;
}
public static void main(String[] args) {
int size = 9;
for (int i = 0; i < size; i++) {
System.out.println(sp(abs((size - 1) / 2 - i)) +
st(abs(9 - 2 * ((i + 5) % (size)))) +
sp(abs((size - 1) / 2 - i)));
}
}
}
输出:
*
***
*****
*******
*********
*******
*****
***
*
试试这个代码。 使用 Math.abs 会简单很多。
import java.util.Scanner;
public class MakeDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Let's Creat Diamonds");
System.out.println("If number increases Diamonds gets bigger. " +
"Please input number lager than 1 : ");
int user_input = sc.nextInt(); //gets user's input
System.out.println("");
int x = user_input;
int front_space = -5;
for (int i = 0; i < 2 * user_input + 1; i++) {
for (int a = front_space; a < Math.abs(i - user_input); a++) {
System.out.print(" ");
}
if (i < user_input + 1) {
for (int b = 0; b < 2 * i + 1; b++) {
System.out.print("* ");
}
} else if (i > user_input) {
for (int c = 0; c < 2 * x - 1; c++) {
System.out.print("* ");
}
x--;
}
System.out.print('\n');
}
System.out.println("\nRun Again? 1 = Run, 2 = Exit : ");
int restart = sc.nextInt();
System.out.println("");
if (restart == 2) {
System.out.println("Exit the Program.");
System.exit(0);
sc.close();
}
}
}
}
您可以使用for
循环打印星号(数学运算符)的菱形:
int m = 4;
int n = 4;
for (int i = -m; i <= m; i++) {
for (int j = -n; j <= n; j++) {
int val = Math.abs(i) + Math.abs(j);
System.out.print(val > Math.max(m, n) ? " " : "∗");
if (j < n) {
System.out.print(" ");
} else {
System.out.println();
}
}
}
输出:
∗
∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗
∗
您可以使用IntStream
s创建一个带有星号菱形的二维数组:
int m = 4;
int n = 4;
String[][] arr = IntStream.rangeClosed(-m, m)
.mapToObj(i -> IntStream.rangeClosed(-n, n)
.map(j -> Math.abs(i) + Math.abs(j))
.mapToObj(j -> j > Math.max(m, n) ? " " : "∗")
.toArray(String[]::new))
.toArray(String[][]::new);
// formatted output
Arrays.stream(arr)
.map(row -> Arrays.stream(row)
.collect(Collectors.joining(" ", "[ ", " ]")))
.forEach(System.out::println);
[ ∗ ]
[ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ∗ ∗ ]
[ ∗ ∗ ∗ ]
[ ∗ ]
也可以看看:
•带有数字的空菱形
•用菱形形式的数字填充二维数组
您可以使用两个嵌套的for 循环和一个if else 语句,如下所示:
public static void main(String[] args) {
int n = 5;
for (int i = -n; i <= n; i++) {
for (int j = -n; j <= n; j++)
if (Math.abs(i) + Math.abs(j) <= n
// in chessboard order
&& (i + j) % 2 != 0)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}
}
输出:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
试试这个代码:
我改变了第一个循环:
for (int i = 1; i <= size-1; i += 2) {
int size = 9;
for (int i = 1; i <= size - 1; i += 2) {
for (int k = size; k >= i; k -= 2) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}// end loop
for (int i = 1; i <= size; i += 2) {
for (int k = 1; k <= i; k += 2) {
System.out.print(" ");
}
for (int j = size; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}// end loop
这段代码工作正常。 只是你需要删除一个重复两次的额外行......
class Diamond {
public static void main(String[] args) {
int size = 9;
for (int i = 1; i <= size; i += 2) {
for (int k = size; k >= i; k -= 2) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}// end loop
for (int i = 1; i <= size; i += 2) {
for (int k = 1; k <= i + 2; k += 2) { // I made change here
System.out.print(" ");
}
for (int j = size - 2; j >= i; j--) { // I made change here
System.out.print("*");
}
System.out.println();
}// end loop
}
}
输出:
*
***
*****
*******
*********
*******
*****
***
*
另一个有趣的答案是尝试 JDK16 记录。 这个菱形形状Math.abs
在一行中循环遍历不同的索引, Math.abs
在简单的声明中使用Math.abs
:
public record Diamond(int midx, int midy, int size) implements Shape {
public int right() { return midx + size; }
public int top() { return midy + size; }
/** Check if shape is drawn at this x,y position */
public boolean intersects(int x, int y) {
return Math.abs(midx-x) + Math.abs(midy-y) <= size;
}
}
添加所有Shape
类的接口和通用draw
以打印任何相交形状的星号:
public interface Shape {
/** Check if shape is drawn at this x,y position */
boolean intersects(int x, int y);
/** Max X position used by this shape */
int right();
/** Max Y position used by this shape */
int top();
/** Draw a series of shapes */
public static void draw(Shape ... shapes) {
// Work out largest X, Y coordinates (and print the shape list):
int ymax = Arrays.stream(shapes).peek(System.out::println)
.mapToInt(Shape::top ).max().getAsInt();
int xmax = Arrays.stream(shapes).mapToInt(Shape::right).max().getAsInt();
System.out.println();
// Visit all X,Y and see what will be printed
for (int y = ymax ; y > 0; y--) {
for (int x = 1 ; x <= xmax; x++) {
boolean hit = false;
for (int i = 0; !hit && i < shapes.length; i++) {
hit = shapes[i].intersects(x,y);
}
System.out.print(hit ? "*" : " ");
}
System.out.println();
}
System.out.println();
}
}
...还有一个主要绘制任意数量的形状 - 定义新的 ASCII 艺术Shape
类很容易:
public static void main(String[] args) {
Shape.draw(new Diamond(10, 7, 5));
Shape.draw(new Diamond(10, 7, 3), new Diamond(17, 5, 3), new Diamond(22, 8, 1));
}
这打印:
Diamond[midx=10, midy=7, size=5]
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Diamond[midx=8, midy=7, size=5]
Diamond[midx=17, midy=5, size=3]
Diamond[midx=22, midy=8, size=1]
*
***
*****
******* *
********* * ***
*********** *** *
********* *****
******* *******
***** *****
*** ***
* *
另请参阅:绘制两棵特定高度的 ASCII 云杉树
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.