본문 바로가기
ONLINE COURSES/Java

인프런 자바기초강의_5,6,7,8강

by jono 2021. 7. 15.

0. 형 변환

1. 자동적 형변환 (묵시적 형변환)
: 작은 공간의 메모리에서 큰 공간의 메모리로 이동.

2. 명시적 형변환 
:  큰 공간의 메모리에서 작은 공간의 메모리로 이동. => 명시해줘야 성립한다.

int iVar = 100;
byte bVar = (byte)iVar;

* 명시적 형 변환은 데이터가 누실될 수 있다.

따라서 주로 처음부터 int, double 등 큰 자료형을 사용한다.


5강

5-2 서식문자
: 일반 문자가 아닌 서식에 사용되는 문자

System.out.println("오늘의 기온은 10도 입니다.");
System.out.printf("오늘의 기온은 %d도 입니다.\n", 10);

int num1 = 20;
System.out.println("오늘의 기온은 " + num1 + "도 입니다.");
System.out.printf("오늘의 기온은 %d도 입니다.\n",num1);

System.out.printf("홍길동's info: %d학년 %d반 %d번\n" , 6,1,17);

/* 정수
* %d -> 10진수
* %o -> 8진수
* %x -> 16진수
*/
int num2 = 30;
System.out.printf("10진수일때는 %d\n", num2);
System.out.printf("8진수일 때는 %o\n", num2);
System.out.printf("16진수일 때는 %x\n", num2);


/* 문자 %c  with 작은따옴표!   '  '
* 문자열 %s with 큰따옴표!    "  "
* */
System.out.printf("소문자 \'%c\'의 대문자는 \'%c\' 입니다.\n", 'a', 'A');
System.out.printf("소문자 \'%s\'를 대문자로 바꾸면 \'%s\' 입니다.\n", "java", "JAVA");


/*실수 
* float,double -> %f
*/
float f = 1.23f;
System.out.printf("f = %f\n",f);

double d = 1.2345d;
System.out.printf("d = %f\n", d);

5-3 서식 문자의 정렬과 소수점 제한 기능

%d -> 왼쪽정렬
%5d -> 왼쪽에서부터 5칸띄워서 정렬

%f   -> 전체 숫자 출력
%.0f  -> 정수만 출력
%.1f  -> 소숫점1자리까지 출력
%.2f  -> 소숫점 2자리까지
%.3f  -> 소숫점 3자리까지


6강. 연산자

6-3 산술 연산자

/ -> 나눗셈
% -> 나머지

6-4 복합 대입 연산자

int x = 10;
system.out.println("x += : " + (x += 10));   // x += : 20

 

6-6 증감 연산자

- 전위연산자 (++x) : 먼저 +1부터 처리한 후 출력된다 
- 후위연산자 (x++) : x값을 먼저 출력한 후 +1은 메모리상에서만 일어난다.

int x = 10;
system.out.println(" ++x : " + (++x));  // 11
system.out.println(" x++ : " + (x++));  // 10

 

6-8 조건(삼항) 연산자

조건식 ? 식1 : 식2

int a =10;
int b =20;
int result1 = 0;
result1 = (a>b) ? 100 : 200;
System.out.println("a>b 이면 100을 출력 : " + result1);

String result2 = "";
result2 = (a<b)? "true" : "false";
System.out.println("a<b 인가?: " + result2);  // a<b 인가?: true

String result3 = "";
result3 = (a==b) ? "true" : "false";
System.out.println("a==b 인가?: " + result3);   // a==b 인가?: false

6-9 비트 연산자


7강. 배열

*** java에서는 배열의 크기가 한번 정해지면 후에 변경될 수 없다!

7-2 배열 선언 및 초기화

- 배열선언 
 자료형[]배열이름 = new자료형[n]; 
* 인덱스는 0부터 n-1이다.

int[] arr1 = new int[5]; // arr1 의 이름, 메모리공간이 5인 배열을 선언함

 

- 배열 초기화

int[] arr1 = new int[5];
arr1[0] = 100;
arr1[1] = 200;
arr1[2] = 300;
arr1[3] = 400;
arr1[4] = 500;

- 배열의 선언과 초기화를 동시에 하기. 
 System.out.println(Arrays.toString(배열이름));  => 해당 배열 전체를 출력한다.

int[] arr2 = {100,200,300,400,500};
System.out.println(Arrays.toString(arr2));

7-3 배열을 이용한 학사관리 프로그램

1. name 이라는 문자배열을 선언한다

//name이라는 문자배열 선언
String[] name = {"김가나" , "이다라", "박마바" , "정사아", "홍자차"};
int[] score = new int[5];

2. 점수를 input으로 받아 score배열에 저장한다.

***Scanner 에 대해서는 배우지 않았음  ( 하단 참고 )
(System.in을 통해 input을 받고 -> name[0]의 value로 지정된 후 -> score[0] 값으로 지정되는것인듯..?)
-> 

//데이터 입력
Scanner scanner = new Scanner(System.in);
System.out.printf("%s의 점수를 입력하시오. -> ", name[0]);
score[0] = scanner.nextInt();

System.out.printf("%s의 점수를 입력하시오. -> ", name[1]);
score[1] = scanner.nextInt();

System.out.printf("%s의 점수를 입력하시오. -> ", name[2]);
score[2] = scanner.nextInt();

System.out.printf("%s의 점수를 입력하시오. -> ", name[3]);
score[3] = scanner.nextInt();

System.out.printf("%s의 점수를 입력하시오. -> ", name[4]);
score[4] = scanner.nextInt()

//데이터 출력
System.out.printf("%s의 점수: %.2f\n", name[0], (double)score[0]);
System.out.printf("%s의 점수: %.2f\n", name[1], (double)score[1]);
System.out.printf("%s의 점수: %.2f\n", name[2], (double)score[2]);
System.out.printf("%s의 점수: %.2f\n", name[3], (double)score[3]);
System.out.printf("%s의 점수: %.2f\n", name[4], (double)score[4]);

3. 입력한 점수들의 평균구하기

double ave = (double)(score[0]+score[0]+score[0]+score[0]+score[0]) / score.length;
System.out.printf("--------------------\n평점: \t%.2f",ave);

scanner.close();

System.out.println();

7-4 배열의 길이. 출력, 복사, 레퍼런스, 다중배열

-배열의 길이, 출력, 복사

int[] arrAtt1 = {10,20,30,40,50};
int [] arrAtt2 = null;
int [] arrAtt3 = null;

//배열의 길이
System.out.println(arrAtt1.length);  //5

//배열 요소 출력
System.out.println(Arrays.toString(arrAtt1));  
System.out.println(Arrays.toString(arrAtt2));  //null출력

//배열 요소 복사    복사결과물이될 배열의 이름 = Arrays.copyOf(복사대상, 복사대상.length);
arrAtt3 = Arrays.copyOf(arrAtt1, arrAtt1.length);
System.out.println(Arrays.toString(arrAtt3));

- 배열 레퍼런스

// 배열 레퍼런스
arrAtt2 = arrAtt1;
System.out.println(arrAtt1);   //출력내용은 같아진다
System.out.println(arrAtt2);   //출력내용은 같아진다
System.out.println(arrAtt3);

- 다중배열

//다차원 배열
int [][] arrMulti = new int[3][2];
arrMulti[0][0]= 10;
arrMulti[0][1]= 100;
arrMulti[1][0]= 20;
arrMulti[1][1]= 200;
arrMulti[2][0]= 30;
arrMulti[2][1]= 300;

System.out.println(Arrays.toString(arrMulti[0]));  //[10,100]
System.out.println(Arrays.toString(arrMulti[1]));  //[20,200]
System.out.println(Arrays.toString(arrMulti[2]));  //[30,300]

 

#Scanner문법

Scanner inputNum = new Scanner(System.in);
int score = inputNum.nextInt();
inputNum.close();

댓글