on
20211215 - JAVA 1회차 강의 내용
20211215 - JAVA 1회차 강의 내용
자바 설치: jdk 검색 > Oracle > 운영체제 선택 Installer
내PC > C: > Program Files > java > jdk-17.0.1 > bin > java.exs, javac.exe(컴파일러)
어디에서나 사용할 수 있게 패스 설정
내PC > 속성 > 고급 시스템 설정 > 환경 변수 > 시스템 변수 > Path 편집
C:\~\bin 경로 새로만들기로 등록
커맨드창, 메모장으로 주로 수업(현업에서 많이 쓰는 건 이클립스)
자바는 대소문자를 구별한다.
1 2 3 4 5 6 7 8 class Hello { public static void main( String [] args) { System . out . println ( "Hello World" ); //ln 개행한다는 의미(출력하고 줄바꿈) } } Colored by Color Scripter cs
1 2 3 4 5 6 7 class Good { public static void main( String [] args) { System . out . println ( "Good" ); } } Colored by Color Scripter cs
위 기본 구조 외우기
기본 구조 class Hello {} //Hello는 클래스 이름, 대문자로 시작하는게 암묵적 룰
main 메소드는 반드시 필요
메소드 앞에 반환하는 형을 명시해야 한다.
public static void main: 반환하지 않음을 의미
String 문자열 클래스
메모장 저장 시 ANSI로 저장해야 윈도우에서 안 깨진다.
dir 목록보기 명령 이 붙지 않은 건 그냥 파일
. 현재 디렉토리
.. 상위 디렉토리
//점 한개, 두개는 무조건 나옴
cd. 현재 디렉토리로 위치를 바꾸는 명령
cd.. 상위
cd \ 한번에 최상위 드라이브로 가기
cd c:\1215\Day1 한번에 원하는 곳으로 갈 수 있는 절대경로
javac Hello.java 컴파일러로 이 파일을 연다. > class파일이 생김
cd 1215 cd Day1
중요한 부분: 데이터타입(자료형), 가독성
읽기 좋은 코드가 좋은 코드(들여쓰기, 띄어쓰기)
명령어 println > print : 개행하지 않는다.
.java 파일 내용이 바뀌면 cmd에서 컴파일도 새로 해줘야 반영된다.
cmd 위, 아래 화살표로 이전 명령어를 기억한다
1 2 3 4 5 6 7 8
9
10 class JavaAdd { public static void main( String [] args) { System . out . println ( 2 + 3 ); //5로 출력 System . out . println ( "2+3" ); //2+3 그대로 출력 System . out . println ( 2+3+"Hello" ); //5Hello
System . out . println ( "Hello"+2+3 ); //Hello23
} } Colored by Color Scripter cs
2+3+"Hello" > 왼쪽->오른쪽 순으로 연산
2+3까지 연산하고 자료형이 달라서 연산 불가 > 그대로 출력
"Hello"+2+3 > 가장 왼쪽부터 계속 연산 불가하여 그대로 출력 > Hello23
For 반복문을 배우기 위해 변수를 먼저 알아야 한다.
int 정수(소수점이 없는 숫자)를 저장하는 자료형
= 대입연산자
1 2 3 4 5 6 7 8 9 10 class Variable { public static void main( String [] args) { int num = 10 ; //선언 System . out . println (num); //10 num = 100 ; System . out . println (num); //100 } } Colored by Color Scripter cs
1 int i = 10, j = 20; cs
동일한 중괄호 안에서 동일한 이름의 변수를 선언할 수 없다.
자료형이 동일할 경우 한 줄에서 한번에 선언 가능
1 2 3 4 5 6 7 8 9 10 11 class HiRep { public static void main( String [] args) { int i = 0 ; for (i = 0 ;i < 3 ;i = i + 1 ) { System . out . println (i + "Hi" ); } } } Colored by Color Scripter cs
대입연산자는 연산자 우선 순위가 가장 낮다.
1부터 100까지 더하기
1. 더할 수를 구하기 > 변수 i
2. 구한 수를 더하기 > 변수 sum
1 2 3 4 5 6 7 8 9 10 11 12 class Add100 { public static void main( String [] args) { int i = 0 , sum = 0 ; for (i = 1 ;i < = 100 ;i = i + 1 ) { sum = sum + i; System . out . println ( "i=" + i + " sum=" + sum); } } } Colored by Color Scripter cs
1. 1부터 100까지의 합
1 2 3 4 5 6 7 8 9 10 11 12 class Add100 { public static void main( String [] args) { int i = 0 , sum = 0 ; for (i = 1 ;i < = 100 ;i = i + 1 ) { sum = sum + i; System . out . println ( "i=" + i + " sum=" + sum); } } } Colored by Color Scripter cs
2. 1부터 100까지의 짝수의 합
1 2 3 4 5 6 7 8 9 10 11 12 class AddEven100 { public static void main( String [] args) { int i = 0 , sum = 0 ; for (i = 2 ;i < = 100 ;i = i + 2 ) { sum = sum + i; System . out . println (sum); } } } Colored by Color Scripter cs 3. 1부터 100까지의 4의 배수의 갯수
1 2 3 4 5 6 7 8 9 10 11 12 class Multi4 { public static void main( String [] args) { int i = 0 , cnt = 0 ; for (i = 4 ;i < = 100 ;i = i + 4 ) { cnt = cnt + 1 ; System . out . println (sum + "개" ); } } } Colored by Color Scripter cs 4. 7의 배수의 합이 최초로 1000을 넘는 수가 몇번째 항인지
5. 5!
1 2 3 4 5 6 7 8 9 10 11 12 class Fac { public static void main( String [] args) { int i = 0 , fac = 1 ; for (i = 1 ;i < = 5 ;i = i + 1 ) { fac = fac * i; System . out . println (i + "! " + fac); } } } Colored by Color Scripter cs
from http://960603.tistory.com/326 by ccl(A) rewrite - 2021-12-15 22:01:30