2020년 10월 13일 숙제
업데이트:
package com.kh.control.loop.practice;
import java.util.Scanner;
public class LoopPractice {---
public void practice1() {
Scanner sc = new Scanner(System.in);
System.out.print("1 이상의 숫자를 입력하세요 : ");
int input = sc.nextInt();
if(input >= 1) {
for(int i = 1; i <= input; i++) {
System.out.print(i + " ");
}
} else {
System.out.println("1 이상의 숫자를 입력해주세요.");
}
}
public void practice2() {
Scanner sc = new Scanner(System.in);
System.out.print("1 이상의 숫자를 입력하세요 : ");
int input = sc.nextInt();
if(input >= 1) {
for(int i = input; i >= 1; i--) {
System.out.print(i + " ");
}
} else {
System.out.println("1 이상의 숫자를 입력해주세요.");
}
}
public void practice3() {
Scanner sc = new Scanner(System.in);
System.out.print("정수를 하나 입력하세요 : ");
int input = sc.nextInt();
int sum = 0;
for(int i = 1; i <= input; i++) {
if (i != input) {
System.out.print(i + " + ");
} else {
System.out.print(i + " = ");
}
sum += i;
}
System.out.println(sum);
}
public void practice4() {
Scanner sc = new Scanner(System.in);
System.out.print("첫 번째 숫자 : ");
int num1 = sc.nextInt();
System.out.print("두 번째 숫자 : ");
int num2 = sc.nextInt();
int temp = 0;
if(num1 <= 0 || num2 <= 0) {
System.out.println("1 이상의 숫자를 입력해주세요."); return;
} else if(num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
for(int i = num1; i <= num2; i++) {
System.out.print(i + " ");
}
}
public void practice5() {
Scanner sc = new Scanner(System.in);
System.out.print("숫자 : ");
int num = sc.nextInt();
System.out.println("==== " + num + "단 ====");
for(int i = 1; i <= 9; i++) {
System.out.printf("%d * %d = %d\n", num, i, num*i);
}
}
public void practice6() {
Scanner sc = new Scanner(System.in);
System.out.print("숫자 : ");
int num = sc.nextInt();
if (num < 10) {
for (int i = num; i <= 9; i++) {
System.out.println("==== " + i + "단 ====");
for (int j = 1; j <= 9; j++) {
System.out.printf("%d * %d = %d\n", i, j, num * j);
} System.out.println();
}
} else {
System.out.println("9 이하의 숫자만 입력해주세요.");
}
}
public void practice7() {
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int num = sc.nextInt();
for(int row = 1; row <= num; row++) {
for(int col = 1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}
}
public void practice8() {
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int num = sc.nextInt();
int count = num;
for(int row = num; row >= 1; row--) {
for(int col = row; col >= 1; col--) {
System.out.print("*");
}
System.out.println();
}
}
public void practice9() {
Scanner sc = new Scanner(System.in);
System.out.print("자연수 하나를 입력하세요 : ");
int num = sc.nextInt();
int count = 0;
for(int i = 2; i <= num; i++) {
if(i % 2 == 0 || i % 3 == 0) {
System.out.print(i + " ");
if(i % 2 == 0 && i % 3 == 0) {
count++;
}
}
}
System.out.print("\ncount : " + count);
}
public void practice10() {
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int num = sc.nextInt();
for (int i = 1; i <= num; i++) {
for (int j = num - i; j > 0; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
public void practice11() {
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int input = sc.nextInt();
int num = input * 2 - 1;
int count = 1;
for (int i = 1; i <= num; i++) {
if (i <= input) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
} else {
for (int k = input - count; k >= 1; k--) {
System.out.print("*");
}
count++;
System.out.println();
}
}
}
public void practice12() {
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int input = sc.nextInt();
int star = input * 2 - 1;
for(int i = 1; i <= input; i++) {
for(int j = input - i; j > 0; j--) {
System.out.print(" ");
}
for(int k = 1; k <= star; k+=2) {
System.out.print("*");
}
System.out.println();
}
}
}
댓글남기기