10 barrages of drawing with ● or ■ that are likely to appear in training (Java)

(common part)


package test;					
					
import java.util.LinkedList;					
import java.util.List;					
import java.util.Scanner;					
					
public class Main {									
	public static void main(String[] args) {				
		Scanner scan = new Scanner(System.in);			
		int num2 = scan.nextInt();			
		String square = "■";			

		//code

	}
}

** Screenshots are all when the input value is 9. ** **

pattern 1

image.png

//Start at 1 and increment to the number entered
//* Line is the current number of lines and at the same time represents the number of drawings in ■.
for(int line = 1; line <= num2; line++ ){				
	//Start from 1 and repeat for less than or equal to the number of drawings on this line
	for(int idxOfMarks = 1; idxOfMarks <= line; idxOfMarks++){			
		System.out.print(square);		
	}			
	//Line break to go to the next line
	System.out.println("");			
}				

The following is a brief description.

StringBuilder sb = new StringBuilder();				
for (int line = 0; line < num2; line++) {				
    System.out.println(sb.append(square));				
}				

Pattern 2

image.png

//Start with the number you enter and decrement until that number reaches 1.
//* Idx is the current number of lines and at the same time represents the number of drawings in ■
for(int line = num2; line >= 1 ; line--){				
	for(int idxOfMarks = line; idxOfMarks >= 1; idxOfMarks--){			
		System.out.print(square);		
	}			
	System.out.println("");			
}				

Pattern 3

image.png

String batsu = "×";				
for(int line = 1; line <= num2; line++ ){				
	//Start from 1 and repeat for less than or equal to the number of drawings on this line
	for(int idxOfMarks = 1; idxOfMarks <= line; idxOfMarks++){			
		if(idxOfMarks % 2 !=0){		
			System.out.print(square);	
		}else{		
			System.out.print(batsu);	
		}		
	}			
	System.out.println("");			
}	

Pattern 4

image.png

for(int line = 1; line <= num2; line++){				
	for(int idxOfMarks = 1; idxOfMarks <= line; idxOfMarks++){			
		//If the row is odd
		if(line % 2 != 0){		
			//When it becomes an odd number mark
			if(idxOfMarks % 2 != 0){	
				System.out.print(square);
			//When it becomes an even number mark
			}else{	
				System.out.print(batsu);
			}	
		//If the row is even
		}else{		
			//When it becomes an odd number mark
			if(idxOfMarks % 2 == 0){	
				System.out.print(square);
			//When it becomes an even number mark
			}else{	
				System.out.print(batsu);
			}	
		}		
	}			
	System.out.println("");			
}				

Pattern 5

image.png

for(int line = num2; line >=1; line--){				
	//The number of blanks displayed is the number of inputs-Ask in the current line
	int empty = num2 - line;			
	//First, output the blank
	for(int n=1 ;n <=empty; n++){			
		System.out.print(" ");		
	}			
	//Then print the mark
	for(int idxOfMarks = line; idxOfMarks >= 1 ; idxOfMarks--){			
		System.out.print(square);		
	}			
	System.out.println("");			
}				

Pattern 6

image.png

for(int line = 1 ; line<= num2 ; line++){				
	System.out.print(square);			
}				
System.out.println("");				
				
int emptyLines = num2 - 2;				
for(int line2 = 1; line2 <= emptyLines; line2++){				
	for (int index2 = 1 ; index2 <= num2; index2++){			
		if(index2 == 1 || index2 == num2){		
			System.out.print(square);	
		}else{		
			System.out.print(" ");	
		}		
	}			
	System.out.println("");			
}				
for(int line = 1 ; line<= num2 ; line++){				
	System.out.print(square);			
}				

Pattern 7

image.png

for(int idx = 1; idx <= num2; idx++){				
	for(int i = 1; i <= num2-idx; i++){			
		System.out.print(" ");		
	}			
				
	for(int idxOfMarks = 1; idxOfMarks <= idx*2-1; idxOfMarks++){			
		System.out.print(square);		
	}			
				
	for(int i = 1; i <= num2-idx; i++){			
		System.out.print(" ");		
	}			
	System.out.println();			
}				

Pattern 8

image.png

StringBuilder sb3 = new StringBuilder();				
for (int line = 0; line < num2; line++) {				
    System.out.println(sb3.append(square));				
}				
for (int line = 0; line < num2; ++line){				
    System.out.println(sb3.deleteCharAt(0));				
}				

Pattern 9

image.png

    for(int line = 1 ; line <= num2; line++){					
    	for(int idxOfNum=1; idxOfNum<=line; idxOfNum++){				
    		if(line == 1 || line == 2 || line == num2){			
    			System.out.print("●");		
    		}else{			
    			if(idxOfNum == 1 || idxOfNum == line){		
    				System.out.print("●");	
    			}else{		
    				System.out.print("○");	
    			}		
    		}			
    	}				
    	System.out.println();				
    }	

Pattern 10

image.png

    //When the number of lines increases by 1, the position of ● on the left becomes+1, the position of ● on the right is-It becomes 1.
    int idxOfBlack1 = 1;					
    int idxOfBlack2 = num2;					
    for(int line = 1 ; line <= num2; line++){					
    	for(int idxOfNum = 1; idxOfNum<=num2; idxOfNum++){				
    		if(idxOfNum == idxOfBlack1 | idxOfNum == idxOfBlack2){			
    			System.out.print("●");		
    		}else{			
    			System.out.print("○");		
    		}			
    	}				
    	idxOfBlack1++;				
    	idxOfBlack2--;				
    	System.out.println();				
    }					

(If you have a beautiful writing style, I would appreciate it if you could teach me.)

Recommended Posts

10 barrages of drawing with ● or ■ that are likely to appear in training (Java)
10 barrages of drawing with ● or ■ that are likely to appear in training (Java)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
Features that are likely to enter Java 10 for now
A concise summary of Java 8 date / time APIs that are likely to be used frequently
[Java] I want to check that the elements in the list are null or empty [Collection Utils]
Consideration and coping with the fact that SSL communication errors are more likely to occur from Java 11
[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition
[Java] The problem that uploaded images are not updated due to the influence of cache
Summary of how to use the proxy set in IE when connecting with Java
How to convert A to a and a to A using AND and OR in Java
[#FizzBuzz] Write in various languages that "count numbers from 1 to 100 and become stupid only when they are multiples of 3 and numbers with 3"
Summary of how to implement default arguments in Java
Memo that transitions to the login screen if you are not logged in with devise
Whether to make the server side at the time of system rebuild with Kotlin or Java
How to use trained model of tensorflow2.0 with Kotlin / Java
How to handle exceptions coolly with Java 8 Stream or Optional
Identify threads in the Java process that are wasting CPU
Initialization with an empty string to an instance of Java String type
Things to be aware of when writing code in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
How to derive the last day of the month in Java
I tried to make a client of RESAS-API in Java
Recommendation of junior training. ~ To avoid that spicy all-night rush ~
Be sure to compare the result of Java compareTo with 0
Let's try the implementation to find the area of the triangle that we did in the training for newcomers
How to handle items in GSON that do not know whether they are single or array
[Java] How to search for a value in an array (or list) with the contains method
How to make a key pair of ecdsa in a format that can be read by Java
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 1]