
1번문제 코드리뷰(강사님이 해주신 답)
package Day01;
import java.util.Scanner;
public class StarPrinter01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 1번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
for(int i = 1; i <= lineNumber; i++){
for(int j = 1; j <= i; j++){
System.out.print('*');
}
System.out.println();
}
scanner.close();
}
}
2번 문제 코드리뷰(강사님이 해주신 답)
package Day01;
import java.util.Scanner;
public class StarPrinter02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 2번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
for(int i = 1; i <= lineNumber; i++){
String stars = "";
for(int j = i; j <= lineNumber; j++){
stars += "*";
}
System.out.println(stars);
}
scanner.close();
}
}
3번 문제 코드리뷰(강사님이 해주신 답)
package Day01;
import java.util.Scanner;
public class StarPrinter03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 3번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
for(int i = 1; i <= lineNumber; i++){
String stars = "";
for(int j = 1; j <= lineNumber; j++){
if (j <= lineNumber - i) {
stars += " ";
}
else {
stars += "*";
}
}
System.out.println(stars);
}
scanner.close();
}
}
4번 문제 코드리뷰(내가 혼자 풀어본 답)
package Day01;
import java.util.Scanner;
public class StarPrinter04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 4번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
for(int i = lineNumber; i >= 1; i--){
String stars = "";
for(int j = 1; j <= lineNumber; j++){
if (j <= lineNumber - i) {
stars += " ";
}
else {
stars += "*";
}
}
System.out.println(stars);
}
scanner.close();
}
}
5번 문제 코드리뷰(강사님이 해주신 답)
package Day01;
import java.util.Scanner;
public class StarPrinter05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 5번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
for(int i = 1; i <= lineNumber; i++){
String stars = "";
for(int j = 1; j <= lineNumber - i; j++){
stars += " ";
}
for(int j = 1; j <= i * 2 - 1; j++){
stars += "*";
}
System.out.println(stars);
}
scanner.close();
}
}
6번 문제 코드리뷰(강사님이 해주신 답) 쉬운버전
package Day01;
import java.util.Scanner;
public class StarPrinter06_easy {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 6번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
for(int i = lineNumber; i >= 1; i--){
String stars = "";
for(int j = 1; j <= lineNumber - i; j++){
stars += " ";
}
for(int j = 1; j <= i * 2 - 1; j++){
stars += "*";
}
System.out.println(stars);
}
scanner.close();
}
}
6번 문제 코드리뷰(강사님이 해주신 답) 어려운버전
package Day01;
import java.util.Scanner;
public class StarPrinter06_hard {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 6번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
for(int i = 1; i <= lineNumber; i++){
String stars = "";
for(int j = 1; j <= i - 1; j++){
stars += " ";
}
// -2 * i + (2 * lineNumber + 1)
// = 2 * (lineNumber - i) + 1
for(int j = 1; j <= 2 * (lineNumber - i) + 1; j++){
stars += "*";
}
System.out.println(stars);
}
scanner.close();
}
}
7번 문제 코드리뷰(내가 혼자 풀어본 답)
package Day01;
import java.util.Scanner;
public class StarPrinter07 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 7번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
int totalHeight = 2 * lineNumber - 1;
for (int i = 1; i <= totalHeight; i++) {
String stars = "";
if (i<lineNumber) {
for (int j = 1; j <= i; j++) {
stars += '*';
}
}
else {
for(int j = 1; j <= i-lineNumber; j++){
stars += " ";
}
// -2 * i + (2 * lineNumber + 1)
// = 2 * (lineNumber - i) + 1
for(int j = 1; j <= 2 * lineNumber - i; j++){
stars += "*";
}
}
System.out.println(stars);
}
scanner.close();
}
}
8번 문제 코드리뷰(내가 혼자 풀어본 답)
package Day01;
import java.util.Scanner;
public class StarPrinter08 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 8번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
int totalHeight = 2 * lineNumber - 1;
for (int i = 1; i <= totalHeight; i++) {
String stars = "";
int spaceWidth = 0;
int starWidth = 0;
if (i < lineNumber) {
for(int j = 1; j <= lineNumber; j++){
if (j <= lineNumber - i) {
stars += " ";
}
else {
stars += "*";
}
}
}
else {
int lowerI = i - lineNumber + 1;
//아랫부분
spaceWidth = lowerI - 1;
starWidth = lineNumber - lowerI + 1;
for(int j = 1; j <= spaceWidth; j++){
stars += " ";
}
// -2 * i + (2 * lineNumber + 1)
// = 2 * (lineNumber - i) + 1
for(int j = 1; j <= starWidth; j++){
stars += "*";
}
}
System.out.println(stars);
}
scanner.close();
}
}
9번 문제 코드리뷰(강사님이 해주신 답)
package Day01;
import java.util.Scanner;
public class StarPrinter09 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 9번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
int totalHeight = 2 * lineNumber - 1;
for(int i = 1; i <= totalHeight; i++){
String stars = "";
int spaceWidth = 0;
int starWidth = 0;
if (i < lineNumber) {
//윗부분
spaceWidth = lineNumber - i;
starWidth = 2 * i -1;
for(int j = 1; j <= spaceWidth; j++){
stars += " ";
}
for(int j = 1; j <= starWidth; j++){
stars += "*";
}
}
else {
int lowerI = i - lineNumber + 1;
//아랫부분
spaceWidth = lowerI - 1;
starWidth = 2*(lineNumber - lowerI) + 1;
for(int j = 1; j <= spaceWidth; j++){
stars += " ";
}
// -2 * i + (2 * lineNumber + 1)
// = 2 * (lineNumber - i) + 1
for(int j = 1; j <= starWidth; j++){
stars += "*";
}
}
System.out.println(stars);
}
scanner.close();
}
}
10번 문제 코드리뷰(강사님이 해주신 답)
package Day01;
import java.util.Scanner;
public class StarPrinter10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("별 찍기 10번");
System.out.println("출력할 줄 수를 입력해주세요");
System.out.println("> ");
int lineNumber = scanner.nextInt();
int totalHeight = 2 * lineNumber - 1;
for(int i = 1; i <= totalHeight; i++){
String stars = "";
if (i ==1 || i == totalHeight) {
for(int j = 1; j<= totalHeight; j ++){
stars += "*";
}
}
else {
int starWidth = 0;
if(i < lineNumber){
starWidth = lineNumber - i + 1;
}
else {
int lowerI = i - lineNumber + 1;
starWidth = lowerI;
}
int spaceWidth = totalHeight - starWidth * 2;
for(int j = 1; j <= starWidth; j++){
stars += "*";
}
for(int j = 1; j <= spaceWidth; j++){
stars += " ";
}
for (int j = 1; j <= starWidth; j++){
stars += "*";
}
}
System.out.println(stars);
}
scanner.close();
}
}
'JX405기_비트 > Java' 카테고리의 다른 글
| Day02-1 클래스 학습 (0) | 2023.01.12 |
|---|---|
| Day01-3 메소드 예제 풀어보기(2) (0) | 2023.01.09 |
| Day01-3 메소드 예제 풀어보기 (1) (0) | 2023.01.09 |
| Day01-2 메소드 공부하기 (0) | 2023.01.09 |
| Day01-0 List_of_Java_keywords (0) | 2023.01.09 |