· 한줄 주석 - //주석 (ctrl + /)
· 범위 주석 - /* 주석 */ (ctrl + shift + /)
1. 출력
1) System.out.print(숫자);
2) System.out.print("문자");
3) System.out.print("문자" + "문자");
4) System.out.print(변수명);
5) System.out.println() - 출력 + 줄바꿈 (sysout + ctrl + spacebar)
6) System.out.printf("출력 서식", 출력할 내용);
(1) 출력 서식
· %d - 정수
%5d - 5자리 정수 출력
%05d - 전체 자리수 중 사용되지 않은 자리수 만큼 왼쪽에 0으로 출력 (ex_ 00123)
%.3fd - 소수점 3자리 출력/소수점 4자리에서 반올림 (ex_1.234)
· %f - 실수
· %o - 8진수 정수
· %x - 16진수 정수
· %b - 논리값
· %c - 문자
· %s - 문자열
· %n - 줄바꿈
7) 이스케이프 시퀀스(escape sequence) - " "안에 사용
- 프로그래밍 언어 특성상 표현할 수 없는 기능, 문자를 표현
· \n - 줄바꿈
· \t - 수평 탭(줄 맞춤)
· \v - 수직 탭
· \" - " 사용
· \' - ' 사용
· \\ - 역슬래시 사용
8) 변수
· int - 정수
· double - 실수
· String - 문자
· boolean - 논리형
ex_ int num;
num = 123;
System.out.print(num);
> 123
9) Scan
· import java.util.Scanner; - 기본적으로 import문 필요(Scanner 클래스의 경로가 java.util.Scanner)
· Scanner scan = new Scanner(System.in);
System.in - 사용자로부터 키 입력을 받기위한 저수준 스트림
(1) Scanner 클래스의 메소드
· int - nextInt()
· double - nextDouble()
· String - next()
· String - nextLine() : 입력받은 라인 전체를 문자열 타입으로 반환
next()와 nextLine()의 차이
ex1_ String str;
str = scan.next();
System.out.print("안녕하세요. 저는 홍길동입니다.");
> 안녕하세요.
ex2_ String str;
str = scan.nextLine();
System.out.print("안녕하세요. 저는 홍길동입니다.");
> 안녕하세요. 저는 홍길동입니다.
· boolean - bextBoolean()
ex_ int num;
num = scan.netInt();
System.out.print(num);
2. 연산자
1) 산술 - ( + - * / %)
num1 / num2 - 몫 (num2가 0일땐 오류)
num1 % num2 - 나머지
2) 증감
· ++ increment
· -- decrement
ex_ i++과 ++i의 차이점
int i = 3, j = 0;
j = i++; [ (j=i)++ j에 i를 먼저 대입한 후, i값을 1증가 시켜준다 ]
// j = 3, i = 4
int i = 5, j =0;
j = ++i; [ j=(i++) i를 먼저 1증가 시킨 후, j에 대입한다 ]
// i = 6, j = 6
3) 논리
· && - and
· || - or
· ! - not
· == - 같다
· != - 같지 않다
3. 배열(Array[])
1) 자료형 배열변수[] = new 자료형[index]
ex_ int array[] = new int[5]; -> 정수형 변수 5개 선언
· index - 배열 크기
· 배열 첫번째 값 - 0부터 시작
·
4. if
1) if
if(조건){ // 조건 > true일 경우 결과값, false일 경우 결과값을 내지 않음
결과값
}
2) if else
if( 조건 ){
결과값1 // 조건이 true일 경우 결과값1 출력
} else {
결과값2 // 조건이 false일 경우 결과값2 출력
}
3) if elseif
if( 조건1 ){
결과값1 // 조건1이 true일 경우 결과값1 출력
} else if( 조건2 ){
결과값2 // 조건2가 true일 경우 결과값2 출력
} else if( 조건3 ){
결과값3 // 조건3이 true일 경우 결과값3 출력
} else{
결과값4 // 모든 조건이 false일 경우 결과값4 출력
}
ex_ int number2 = 90;
if(number2 == 100) {
System.out.println("A+ 입니다.");
} else if(number2 >= 90) {
System.out.println("A 입니다.");
} else if(number2 >= 80) {
System.out.println("B 입니다.");
} else if(number2 >= 70) {
System.out.println("C 입니다.");
} else {
System.out.println("재시험입니다.");
}
4) 삼항 연산자
값 = (조건) ? 값1 : 값2; // 조건이 true일 경우 값1, false일 경우 값2
String result = (number >= 70 ) ? "통과입니다.": "재시험입니다.";
System.out.println(result);
5) equals 비교 - String변수에 사용
String str1 = "안녕하세요";
String str2 = "안녕";
str2 = str2 + "하세요";
System.out.println(str2);
if(str1 == str2) {
System.out.println("같은 문자열입니다.");
} else {
System.out.println("다른 문자열입니다.");
}
// str1과 str2 겉으로 보이겐 "안녕하세요"로 같은 문자열 같으나 결과값은 "다른 문자열입니다"로 뜸
// equals 비교 ( 문자열 비교할땐 equals 사용!)
if(str1.equals(str2)) { // str1의 문자열과 str2의 문자열이 같다
System.out.println("equals 같은 문자열입니다");
} else {
System.out.println("equals 다른 문자열입니다");
}
'Java' 카테고리의 다른 글
[Java] Casting (0) | 2022.04.10 |
---|---|
[Java] Break (0) | 2022.04.08 |
[Java] ArrayList (0) | 2022.04.07 |
[Android] 숫자 찾기 게임 (0) | 2022.04.06 |
[Java] 배열 (0) | 2022.02.11 |
댓글