0222 Review

KDU·2022년 2월 22일

자바공부

목록 보기
6/17

charAt(i) - '0';

 이 코드는 아스키코드로 표기된 숫자 1개를 마법같이 int값과 동일하게 만들어준다.
 숫자로 이루어진 String에서 특정한 값을 빼오거나, String에서 숫자만 빼오는 등의 효율적인 작업을 도와주는 소중한 아이.

System.out.print();

 코딩이 복잡해지면 복잡해질수록 입력받은 값들을 먼저 검증하는 디버깅 과정이 중요하다. 정확한 값들을 받아오는지 언제나 확인하는 습관을 기르자.

Math.random(); vs Random.nextInt(i);

 개인적으로 0혹은 1로 시작해 특정 범위까지 랜덤으로 표현해야 할때는 후자가 더 좋은듯싶다. 전자는 따로 메서드 호출이 필요없다는 장점이 있지만, 후자가 뭔가 더 깔끔한 맛이 있는것 같은 느낌..?

String의 hashCode();

 String은 어떤 방식으로 변수가 만들어져도 hashCode();를 통해 주소값을 출력할 때 값이 같으면 hashCode();로 주소값을 불러올때는 같은 값을 출력하도록 java에서 만들어 두었다.
 String을 사용할 때 정확한 주소값이 필요하다면 identityHashCode(var);을 사용할 것.

오늘의 코드

  1. 패턴 2차원 배열 구현기 (짝수or9초과시 배열이 흐트러지기 때문에 안되게 설정, 재시작과 종료 구현)
package study0222;

import java.util.Scanner;

public class Exercise5 {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		while(true) {
			System.out.println("배열 패턴의 행열 길이를 입력하세요.");
			int num = scan.nextInt();
			if(1 == num%2 && 0<num && 10>num) {
				int[][] arr = new int[num][num];

				int a = 0;
				for(int i=0; i<arr.length; i++) {
					int b = 0;
					int half = arr[i].length/2+1;
					for(int j=0; j<arr[i].length; j++) {
						if(j<half) {
							b++;
							arr[i][j] = a+b;
						} else {
							b -= 1;
							arr[i][j] = a+b;
						}
						System.out.print(arr[i][j]+" ");					
					}
					if(i<half-1) {
						a++;
					} else {
						a--;
					}
					System.out.println();
				}
				System.out.println("다시 하시겠습니까? Y/N");
				String n = scan.next();
				if(n.equals("N") || n.equals("n")) {
					System.out.println("종료합니다.");
					scan.close();
					return;
				}
			} else {
				System.out.println("Error : 1~9중 홀수만 입력하세요.");
			}
		}
	}
}
  1. 가위바위보
package study0222;

import java.util.Random;
import java.util.Scanner;

public class FlowTest9 {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		Random random = new Random();
		int user = 0;
		int com = 0;
		String temp = "";
		String restart = "";

		while(true) {

			user = 0;
			while(user==0) {
				System.out.println("가위, 바위, 보 중 하나를 입력하세요.");
				temp = scan.nextLine();
				if(temp.equals("가위")) {
					user = 1;
				} else if(temp.equals("바위")) {
					user = 2;
				} else if(temp.equals("보")) {
					user = 3;
				} else {
					System.out.println("잘못된 입력입니다.");
				}
			}

			com = (random.nextInt(3)+1);
			String sCom;
			if(com == 1) {
				sCom = "가위";
			} else if (com == 2) {
				sCom = "바위";
			} else {
				sCom = "보";
			}

			System.out.printf("당신은 %s를 냈습니다.\n",temp);
			System.out.printf("컴퓨터는 %s를 냈습니다.\n\n",sCom);

			int value = user-com; 
			if(value == 1 || value == -2) {
				System.out.println("당신이 이겼습니다.");
			} else if(value == 2 || value == -1) {
				System.out.println("당신이 졌습니다.");
			} else {
				System.out.println("비겼습니다.");
			}

			System.out.println("다시 하시겠습니까? (Y(1)/N(2))");
			restart = scan.nextLine();
			if(restart.equals("N") || restart.equals("n") || restart.equals("2")) {
				System.out.println("프로그램을 종료합니다.");
				scan.close();
				return;
			}
		}
	}
}
profile
의문을 즐깁니다.

0개의 댓글