package Day5;
import java.util.Random;
import java.util.Scanner;
public class Q1 {
static void main() {
// 1. 호수 -> 물고기 배치/ 물고기 배치를 어떻게 할거냐
// 2. 낚시꾼이 캐스팅 -> 인덱스 범위를 벗어나서 던지면 잘못된 입력
// 2-1. 던지자마자 거기에 물고기가 있으면 1마리 잡고 시작
// 3. 물고기 3마리 다 잡을 때까지 이동
// 3-1. 1. 위 2. 아래 3. 왼쪽 4. 오른쪽
// 주의점: 더이상 이동할 수 없는 인덱스를 이동하면 더이상 움직일 수 없다고 알려줘야함
// 위: 0보다 작으면 잘못됐다
// 오른쪽: 4보다 크면 잘못됐다
// 왼쪽: 0보다 작으면 잘못됐다
// 아래: 4보다 크면 잘못됐다
Scanner sc = new Scanner(System.in);
Random r = new Random();
boolean[][] hosu = new boolean[5][5]; // 초기화 -> false
int fishCount = 0;
for (int i = 0; i < 3; i++) {
int h = r.nextInt(5);
int y = r.nextInt(5);
if (!hosu[h][y]) {
hosu[h][y] = true;
fishCount++;
} else {
i--;
}
}
for (int i = 0; i < hosu.length; i++) {
for (int j = 0; j < hosu.length; j++) {
if (hosu[i][j]) {
System.out.print("🐠\t");
} else {
System.out.print("🌊\t");
}
}
System.out.println();
}
int y = -1;
while (y < 0 || y > 4) {
System.out.print("행 입력: ");
y = sc.nextInt();
}
int x = -1;
while (x < 0 || x > 4) {
System.out.print("열 입력: ");
x = sc.nextInt();
}
// y 0~4 x 0~4
if (hosu[y][x]) {
fishCount--;
hosu[y][x] = false;
}
for (int i = 0; i < hosu.length; i++) {
for (int j = 0; j < hosu.length; j++) {
if (i == y && j == x) {
System.out.print("🧨");
} else if (hosu[i][j]) {
System.out.print("🐠\t");
} else {
System.out.print("🌊\t");
}
}
System.out.println();
}
while (fishCount != 0) {
System.out.print("1. 위 2. 아래 3. 왼쪽 4. 오른쪽 : ");
int num = sc.nextInt();
if (num == 1) {
y--;
if (y == -1) {
System.out.println("더이상 위로 움직일 수 없습니다.");
y = 0;
} else {
if (hosu[y][x]) {
fishCount--;
hosu[y][x] = false;
}
}
} else if (num == 2) {
y++;
if (y == 5) {
System.out.println("더이상 아래로 움직일 수 없습니다.");
y = 4;
} else {
if (hosu[y][x]) {
fishCount--;
hosu[y][x] = false;
}
}
} else if (num == 3) {
x--;
if (x == -1) {
System.out.println("더이상 왼쪽으로 움직일 수 없습니다.");
x = 0;
} else {
if (hosu[y][x]) {
fishCount--;
hosu[y][x] = false;
}
}
} else if (num == 4) {
x++;
if (x == 5) {
System.out.println("더이상 오른쪽으로 움직일 수 없습니다.");
x = 4;
} else {
if (hosu[y][x]) {
fishCount--;
hosu[y][x] = false;
}
}
} else {
System.out.println("잘못된 입력입니다.");
}
for (int i = 0; i < hosu.length; i++) {
for (int j = 0; j < hosu.length; j++) {
if (i == y && j == x) {
System.out.print("🧨\t");
} else if (hosu[i][j]) {
System.out.print("🐠\t");
} else {
System.out.print("🌊\t");
}
}
System.out.println();
}
}
}
}
사실 이 문제도 풀었어야 했는데 나에겐 아직 어려운 문제라 풀지는 못하고 선생님 풀이만 보았다.
package Day5;
// 3명의 학생 점수를 입력받아 최종 점수, 합격 여부 등급을 출력하는 프로그램을 만들어라.
// 점수 입력시 기본 점수와 보너스 점수 입력
// 메소드 1 -> 주고 받고 / 기본 점수와 보너스 점수를 더해서 값을 리턴해주는 기능
// (기본이랑 보너스를 더했을 때 100점이 넘으면 100점으로 처리)
// 메소드 2 -> 주고 안받고 / 통과점수 60점인데 60점을 호출하는 쪽에서 알게만 해주면 된다.
// 메소드 3 -> 안주고 받고 / 최종 점수 출력 및 합격 불합격 출력 학점까지
// 메소드 4 -> sout "===학생 점수 판정 프로그램===" 출력
import java.util.Scanner;
public class Q2 {
// 메소드 1 주고 받고
static int lastScore(int score, int bonus) {
int result = score + bonus;
if (result > 100) {
result = 100;
} else if (result < 0) {
result = 0;
}
return result;
}
// 메소드 2 주고 안받고
static String isPass() {
return "결과: 합격";
}
// 메소드 3 안주고 받고
static void answer() {
}
// 메소드 4
static String name() {
return "===학생 점수 판정 프로그램===";
}
static void main() {
Scanner sc = new Scanner(System.in);
System.out.println(name());
System.out.print("기본 점수 입력: ");
int score = sc.nextInt();
System.out.print("보너스 점수 입력: ");
int bonus = sc.nextInt();
System.out.print("최종 점수: " + lastScore(score, bonus));
System.out.println();
int lastScore = lastScore(score, bonus);
if (lastScore >= 60) {
System.out.print(isPass());
} else {
System.out.print("결과: 불합격");
}
System.out.println();
if (lastScore >= 90) {
System.out.println("등급: A");
} else if (lastScore >= 80) {
System.out.println("등급: B");
} else if (lastScore >= 70) {
System.out.println("등급: C");
} else if (lastScore >= 60) {
System.out.println("등급: D");
} else {
System.out.println("등급: F");
}
}
}
이 문제도 시간이 없어서 3번 메소드를 구현하지 못했다.
시간만 있었으면 완성이 가능했을 거다.
package Day5;
import java.util.Scanner;
public class Q2_1 {
// 1. 주고 받고
static int totalScore(int base, int bonus) {
int finalScore = base + bonus;
if (finalScore > 100) {
finalScore = 100;
}
return finalScore;
}
// 2. 주고 안받고
static int checkScore() {
return 60;
}
// 3. 안주고 받고
static void printResult(int finalScore) {
System.out.println("최종 점수: " + finalScore);
if (finalScore >= checkScore()) {
System.out.println("결과: 합격");
} else {
System.out.println("결과: 불합격");
}
if (finalScore >= 90) {
System.out.println("등급: A");
} else if (finalScore >= 80) {
System.out.println("등급: B");
} else if (finalScore >= 70) {
System.out.println("등급: C");
} else if (finalScore >= 60) {
System.out.println("등급: D");
} else {
System.out.println("등급: F");
}
}
// 4. 안주고 안받고
static void printTitle() {
System.out.println("===학생 점수 판정 프로그램===");
}
static void main() {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("["+(i+1)+"번째 학생]");
System.out.print("기본 점수 입력: ");
int base = sc.nextInt();
System.out.println("보너스 점수 입력: ");
int bonus = sc.nextInt();
int finalScore = totalScore(base, bonus);
printResult(finalScore);
}
}
}
package Day5;
import java.util.Random;
public class Q3 {
// 1. 주고 안받고 랜덤값 추출하는 기능 메소드
static int randomMake() {
Random r = new Random();
return r.nextInt(26);
}
// 2. 주고 받고 더하기 연산 메소드
static int add(int ran) {
return 'A' + ran;
}
// 3. 주고 받고 문자 만드는 메서드
static char makeChar(int result) {
return (char) result;
}
// 4. 안주고 안받고 10번 실행하는 메서드
static void paly() {
String str = "";
for (int i = 0; i < 10; i++) {
int ran = randomMake();
int temp = add(ran);
char ch = makeChar(temp);
str = charAdd(str, ch);
}
resultPrint(str);
}
// 5. 주고 받고 10개 문자를 더하는 메서드
static String charAdd(String str, char ch) {
return str + ch;
}
// 6. 안주고 받고 10개 문자를 출력하는 메서드
static void resultPrint(String result) {
System.out.println(result);
}
static void main() {
paly();
}
}
package Day5;
public class CrCvTest {
// Call by Value -> 메서드
static void callByValue(int a) {
a = 10;
}
// Call by Reference -> 메서드
static void callByReference(int[] arr) {
arr[0] = 10;
}
static int[] testMethod() {
int[] arr = new int[5];
return arr;
}
static void main() {
int a = 30;
callByValue(a);
System.out.println(a);
int[] arr = new int[3];
callByReference(arr);
System.out.println(arr[0]);
}
}
package Day5;
public class MATest {
static int[] a(){
int[] a = new int[3];
return a;
}
static void b(int[] a) {
a[0] = 10;
}
static void c(int[] a) {
a[1] = 20;
}
static void main() {
int[] c = new int[3];
int[] b = a();
b(c);
c(b);
System.out.println(c[0] + "," + c[1] + "," + c[2]);
System.out.println(b[0] + "," + b[1] + "," + b[2]);
}
}
package Day5;
// 무한 반복 프로그램
// 구구단, 별찍기, 종료 화면
// 1번 입력시 -> 원하는 구구단을 입력하세요
// 원하는 구구단을 출력하세요 -> 구구단 출력
// 2번 입력시 -> 1. 네모 2. 직삼각형 정방향 3. 직삼각형 역방향 4. 피라미드 5. 다이아몬드
// 무조건 실행 이후에 첫화면 이동/ 잘못된 번호 입력시 잘못된 입력 출력 함수화
// 입력 출력 연산 관련 분기
import java.util.Scanner;
public class Q4 {
static void gugudan(Scanner sc) {
System.out.print("원하는 구구단을 입력하세요: ");
int user = sc.nextInt();
System.out.println("=== " + user + "단 ===");
for (int i = 2; i <= 9; i++) {
System.out.println(user + " x " + i + " = " + (user * i));
}
}
static void star(Scanner sc) {
System.out.println("1. 네모 2. 직삼각형 정방향 3. 직삼각형 역방향 4. 피라미드 5. 다이아몬드");
int star = sc.nextInt();
if (star == 1) {
squre(sc);
} else if (star == 2) {
triangle(sc);
} else if (star == 3) {
reverseTriangle(sc);
} else if (star == 4) {
pyramid(sc);
} else if (star == 5) {
diamond(sc);
}
}
// 네모
static void squre(Scanner sc) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("* ");
}
System.out.println();
}
}
// 직삼각형 정방향
static void triangle(Scanner sc) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
// 직삼각형 역방향
static void reverseTriangle(Scanner sc) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
// 피라미드
static void pyramid(Scanner sc) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
// 다이아몬드
static void diamond(Scanner sc) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
static void esc(Scanner sc) {
System.out.println("시스템을 종료합니다.");
}
static void wrong(Scanner sc) {
System.out.println("잘못된 입력입니다.");
}
static void main() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. 구구단 출력 2. 별찍기 출력 3. 종료");
System.out.print("번호를 입력하세요: ");
int user = sc.nextInt();
if (user == 1) {
gugudan(sc);
} else if (user == 2) {
star(sc);
} else if (user == 3) {
esc(sc);
break;
} else {
wrong(sc);
}
}
}
}
이건 나 혼자 main문 작성과 메서드화까지 시켰다.
생각보다 시간이 오래걸리고 조금 헤매기도 하였지만 나 혼자 작성했다는 점이 뿌듯하다.
선생님 풀이는 내일 하신다고 하셔서 내일 업로드 하겠다.
package Practice.Day5;
// 숫자 출력 프로그램
// 무한 반복 프로그램을 작성하라.
// 메뉴 -> 1. 짝수 출력 2. 홀수 출력 3. 배수 출력 4. 종료
// 1. 몇 까지 출력할까요? -> 입력한 숫자의 짝수만 출력
// 2. 몇 까지 출력할까요? -> 입력한 숫자의 홀수만 출력
// 3. 숫자를 입력하세요, 배수를 입력하세요 -> 선책한 숫자의 배수 출력
// 4. 프로그램을 종료합니다. 출력
// 잘못된 입력일 시 잘못된 입력입니다를 출력
// 메서드 조건
// even() // 짝수 출력
//odd() // 홀수 출력
//multiple() // 배수 출력
//exit() // 종료
//wrong() // 잘못된 입력
//menu() // 메뉴 출력
import java.util.Scanner;
public class Q1 {
static void menu(Scanner sc) {
System.out.println("1. 짝수 출력 2. 홀수 출력 3. 배수 출력 4. 종료");
System.out.print("숫자를 입력하세요: ");
}
static void even(Scanner sc) {
System.out.print("몇까지 출력할까요?: ");
int even = sc.nextInt();
for (int i = 1; i <= even; i++) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
System.out.println();
}
static void odd(Scanner sc) {
System.out.print("몇까지 출력할까요?: ");
int odd = sc.nextInt();
for (int i = 1; i <= odd; i++) {
if (i % 2 == 1) {
System.out.print(i + " ");
}
}
System.out.println();
}
static void multiple(Scanner sc) {
System.out.print("숫자를 입력하세요: ");
int number = sc.nextInt();
System.out.print("배수를 입력하세요: ");
int multiple = sc.nextInt();
for (int i = 1; i <= number; i++) {
if (i % multiple == 0) {
System.out.print(i + " ");
}
}
System.out.println();
}
static void exit() {
System.out.println("프로그램을 종료합니다.");
}
static void main() {
Scanner sc = new Scanner(System.in);
while (true) {
menu(sc);
int menu = sc.nextInt();
if (menu == 1) {
even(sc);
} else if (menu == 2) {
odd(sc);
} else if (menu == 3) {
multiple(sc);
} else if (menu == 4) {
exit();
break;
} else {
System.out.println("잘못된 입력입니다.");
}
}
}
}
조금 절긴 했지만 거의 혼자서 다 작성하였다.
오늘은 메서드에 대해 깊게 공부하는 시간이었다. 재미있었다.
for문에 조금 더 익숙해지고 메서드화하는 연습을 많이 해야 할 거 같다.