Use for loop to make graphics
One, the code
1. Square
The code is as follows (example):
public class Hello1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <=10 ; j++) {
System.out.print("* ");
}
System.out.println();//换行
}
}
}
2. Rectangle
The code is as follows (example):
public class Hello1 {
public static void main(String[] args) {
for (int i = 1; i <=6 ; i++) {
for (int j = 1; j <=10 ; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
3. Parallelogram
The code is as follows (example):
public class Hello1 {
public static void main(String[] args) {
for (int i = 1; i <=10 ; i++) {
for (int j = 1; j <11-i; j++) {
System.out.print(" ");
}
for (int a = 1; a <=10 ; a++) {
System.out.print("* ");
}
System.out.println();
}
}
}
4. Triangle
The code is as follows (example):
public class Hello1 {
public static void main(String[] args) {
for (int i = 1; i <=10 ; i++) {
for (int j = 1; j <=10-i ; j++) {
System.out.print(" ");
}
for (int a = 1; a <=i ; a++) {
System.out.print(" * ");
}
System.out.println();
}
}
}
5.Rhombus
The code is as follows (example):
public class Hello1 {
public static void main(String[] args) {
for (int i = 1; i <=10 ; i++) {
for (int j = 1; j <=10-i ; j++) {
System.out.print(" ");
}
for (int a = 1; a <=i ; a++) {
System.out.print(" * ");
}
System.out.println();
}
for (int i = 1; i <=9 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print(" ");
}
for (int a = 1; a <=10-i; a++) {
System.out.print(" * ");
}
System.out.println();
}
}
}
6. Hollow diamond
The code is as follows (example):
public class Hello1 {
public static void main(String[] args) {
for (int i = 1; i <=10 ; i++) {
for (int j = 1; j <=10-i ; j++) {
System.out.print(" ");
}
for (int a = 1; a <=i ; a++) {
if (a==1 || a==i){
System.out.print(" * ");
}else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = 1; i <=9 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print(" ");
}
for (int a = 1; a <=10 ; a++) {
if (a==1 || a==10-i){
System.out.print(" * ");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
7. Effect picture
to sum up
The above is to use the for loop in JAVA to realize the content of making graphics, and the main use is the for loop and nesting in JAVA.