(1) ascii code 값 외우기
'0' = 48번
'1 = 49번
'2' = 50번
'A' = 65번
'a' = 97번
(2) **타입변환
자동(묵시적) 타입 변환 : Promotion
강제(명시적) 타입 변환 : Casting
- 자동 타입 변환 : 프로그램 실행 도중에 작은 타입은 큰 타입으로 자동 타입 변환이 된다
byte(1) < short(2) < int(4) < long(8) < float(4) < double(8)
- 강제 타입 변환 : 작은크기타입 = (작은크기타입) 큰크기타입
(원래 값이 보존될 것인지, 손실될 것인지 if문으로 검사!!)
package sec03.exam02_casting;
public class CheckValueBeforeCasing {
public static void main(String[] args) {
/*int i = 128;
byte b = (byte) i;//byte가 -128~127까지만 가능, 결과 : -128
System.out.println(b);*/
int i = 128;
if(i<-128 || i>127) { // 둘 중 하나라도 true면 true
//or if(i<Byte.MIN_VALUE || i>Byte.MAX_VALUE)
System.out.println("byte 타입으로 변환할 수 없습니다.");
System.out.println("값을 다시 확인해 주세요.");
} else {
byte b = (byte) i;
System.out.println(b);
}
}
}
byte 타입으로 변환할 수 없습니다.
값을 다시 확인해 주세요.
ex)
public class CastingTest {
public static void main(String[] args) {
double d = 123.56;
System.out.println("d:" + d);
//int x = d; double은 8byte고, int는 4byte라서 오류!!
int x = (int) d; // casting 연산 (강제형변환) - 기본형은 기본형끼리, 참조형은 참조형끼리만 변환
System.out.println("x:" + x);
d = x; // (자동형변환 - 작은 값을 큰 값에 집어넣는 거니까 오류x)
System.out.println("d:" + d);
/*boolean b = true;
int y = b;
*/ // 값은 타입만 형 변환 가능하기 때문에 오류
char c = 'a';
int y = c;
System.out.println("y:" + y); // ascii 코드 값을 가지기 때문에 ok
c = (char)y; // (강제형변환)
System.out.println("c:" + c);
}
}
---------- run ----------
d:123.56
x:123
d:123.0
y:97
c:a
ex)
package sec01.exam01_variable;
public class VariableExample {
public static void main(String args[]) {
byte var1 = 10;
//var1 = var1 + 1; //산술연산은 int 값으로 나오기 때문에 byte 변수 var1에 대입할 수 x
var1 ++; // or var1 = (byte) (var1 + 1); or int intvar1 = var1 + 1;
System.out.println(var1);
int intValue1 = 10;
int intValue2 = intValue1 / 4; // 정수 / 정수
System.out.println(intValue2); // 값 : 2 정수로 계산됨
int intValue3 = 10;
//int intValue4 = 10 / 4.0; (산술연산이지만 실수값이 들어갔기 때문에 double로 자동 변환)
double doubleValue = intValue3 / 4.0; // 정수 / 실수 = 실수
System.out.println(doubleValue); //값 : 2.5
}
}
** ex1)
/*
가위 바위 보
1. 가위(0), 바위(1), 보(2)
2. 컴퓨터는 0,1,2중 하나의 난수 발생
3. 나는 0,1,2 중 하나의 수를 입력
4. logic
5. 결과 >> 나 : 바위, 컴 : 가위, 결과 : 이겼다.
*/
public class GBB {
public static void main(String[] args) {
//double d = Math.random();//0.0<= Math.random() < 1.0
//System.out.println(d);
int com = (int) (Math.random() * 3);//0 1 2
int my = 1;
int result = (my - com + 2) % 3;
String myStr;
String comStr;
String resultStr;
if(my == 0) {
myStr = "가위";
} else if(my == 1) {
myStr = "바위";
} else {
myStr = "보";
}
switch(com) {
case 0 : comStr = "가위"; break;
case 1 : comStr = "바위"; break;
default : comStr = "보";
}
if(result == 0) {
resultStr = "이겼다.";
} else if(result == 1) {
resultStr = "졌다.";
} else {
resultStr = "비겼다.";
}
System.out.println("나 : " + myStr + ", 컴 : " + comStr + ", 결과 : " + resultStr);//나 : 바위, 컴 : 가위, 결과 : 이겼다.
}
}
ex2) 주사위 던지기
public class IfDiceExample{
public static void main(String[] args) {
int num = (int) (Math.random() * 6) +1; // 1부터 나오게 하려면 +1, 0부터 나오게 하려면 그냥 *만!!
if (num == 1) {
System.out.println ("1번이 나왔습니다.");
} else if (num ==2) {
System.out.println ("1번이 나왔습니다.");
} else if (num ==3) {
System.out.println ("3번이 나왔습니다.");
}else if (num ==4) {
System.out.println ("4번이 나왔습니다.");
}else if (num ==5) {
System.out.println ("5번이 나왔습니다.");
}else {
System.out.println ("6번이 나왔습니다.");
}
}
}
public class SwitchExample{
public static void main(String[] args) {
int num = (int) (Math.random()*6)+1; // 1~6까지 나온다.
switch (num){
case 1: System.out.println("1번이 나왔습니다.");break;
case 2: System.out.println("2번이 나왔습니다.");break;
case 3: System.out.println("3번이 나왔습니다.");break;
case 4: System.out.println("4번이 나왔습니다.");break;
case 5: System.out.println("5번이 나왔습니다.");break;
default: System.out.println("6번이 나왔습니다.");
}
}
}
'Language > Java' 카테고리의 다른 글
19.10.21. (반복문/for문/구구단 소스/중첩 for문/삼각형 출력) (0) | 2021.01.15 |
---|---|
19.10.19. (ECLIPSE 설치/프로젝트 생성/소스파일/컴파일/실행/글꼴변경/라인번호 생성) (0) | 2021.01.15 |
19.10.17. (제어문/조건문/if/if~else/if~else if ~else/switch~case/주민번호 성별/학점 계산 소스) (0) | 2021.01.15 |
19.10.16. (문자열 연결 연산자(+)/주석/연산자/자판기 소스) (0) | 2021.01.15 |
19.10.15. (변수/자료형DataType/접근제어자/명명규칙/자바키워드/변수선언/값대입/초기화) (0) | 2021.01.15 |