Java — for loop print graphics (loop structure)
Description
through the use of double for loop statements, print the following graph:
p>
submit
Sample
the Output
p>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
int i, j, m, t = 1;// m为后半截控制空格个数的变量
for (i = 1; i <= 4; i++) {
for (j = 1; j <= 4 - i; j++) {// 空格
System.out.print(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {// 星号
System.out.print("*");
}
System.out.println();
}
for (i = 3; i >= 1; i--) {
for (j = 1; j <= t; j++) {// 空格
System.out.print(" ");
}
for (j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");// 星号
}
System.out.println();
t++;
}
}
}
div>
Read More:
- JAVA: How to Solve Foreach Loop Remove/add Element Error
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- Java error prompt….. cannot be resolved
- Ternary operator in Java?: error: not a statement
- Explicit and implicit conversion of Java data type
- How to Close the Current Form in JAVA Swing
- Three ways of thread sequence alternate execution in Java lock free programming
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- [Solved] IDEA java compile error: Error:java: Compilation failed: internal java compiler error
- Java error: java.lang.NoSuchMethodError
- [Solved] Java 9 reflection error: java.lang.reflect.InaccessibleObjectException
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- [Solved] Initialization of anonymous inner class member variable causes java.lang.stackoverflowerror
- Java uses single thread pool to realize multi thread sequential execution (non alternating, non synchronous)
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] JAVA OpenCV Startup Error: java.lang.UnsatisfiedLinkError
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown
- [PROJECT] itdage java to get the weather and send text messages
- JAVA 8: How to Convert List to Map