본문 바로가기

프로그래밍/Java (기초)

(23)
메소드의 입력과 출력 메소드의 입력과 출력매개변수와 인자메소드의 입력 값은 매개변수(parameter)를 통해서 이루어진다. public class MethodDemo4 { public static void numbering(int limit) { int i = 0; while (i < limit) { System.out.println(i); i++; } } public static void main(String[] args) { numbering(5); }} (*결과01234)// public static void main(String[] args) { numbering(5); 의 숫자5가 public class MethodDemo4 { public static void numbering(int limit)에서 limit에..
메소드(method)의 형식과 활용 메소드(method) 메소드는 코드를 재사용 할수 있게 해준다. 메소드의 형식 public static void main(String[] args) {return} // 다음 그림처럼 나누어서 코드를 해석할수 있는데윗부분의 경우 밑에 위치한 main 메소드안에 numbering으로 호출이 되고 있으며, 이 numbering의 정의.. 즉 로직은 윗부분에서 구현되어 있다. Mainmain 메소드는 약속과 같다. public static void main(String[] args)가 이끄는 중괄호의 내용은 실행되기를 기대하는 로직이 위치한다. 메소드가 없다면. public static void main(String[] args) { int i = 0; while(i
배열(array) 배열 배열의 생성 변수와 다르게 여러개의 데이터를 저장해 그룹핑 하는데 사용하며. Array를 말한다. public static void main(String[] args) { String[] classGroup = { "최진혁", "최유빈", "한이람", "이고잉" }; } // classGroup은 배열이 담길 변수의 이름이다. String[]은 classGroup에 담을 배열에 담길 데이터의 타입이 문자열의 배열이라는 의미다배열을 선언할때는 대괄호([])를 붙여야하며, 데이터는 중괄화({})에 넣어주며, 쉼표로 데이터들을 구분한다. 배열의 제어 public static void main(String[] args) { String[] classGroup = { "최진혁", "최유빈", "한이람", "..
반복문의 제어(break, continue, 반복문의 중첩) 반복문의 제어 break 반복작업을 중간에 중단시키는 기능을 한다. public static void main(String[] args) { for (int i = 0; i < 10; i++) { if (i == 5) break; System.out.println("Coding Everybody " + i); } } // 반복문의 실행절에 조건문을 추가하여, i의 값이 5가 될때 break를 만나 종료한다. 결과 (* coding everybody 0coding everybody 1coding everybody 2coding everybody 3coding everybody 4) continuebreak와 달리 continue를 만나면 중단했다가 반복문이 끝날때까지 수행한다. public static v..
반복문(while, for) 반복문 loop, interation으로 부르며, 반복적인 작업을 말한다 반복문의 문법 while while(조건){ 반복 실행 영역} public static void main(String[] args) { while (true) { System.out.println("Coding Everybody"); }}// 조건이 true이므로 Coding Everybody를 계속해서 출력한다.// 조건을 false로 바꾸면 아무일도 일어나지 않는다. int i = 0;// i의 값이 10보다 작다면 true, 크다면 false가 된다. 현재 i의 값은 0이기 때문에 이 반복문은 실행된다. while(i
논리 연산자 (and, or, not) 논리 연산자Boolean을 결합하여 코드를 간결하게 만드는 기능을 수행한다. AND (&&) &&는 좌,우항의 값이 모두 true일 때, 참이되며, AND라고 읽는다. if (true && true) { System.out.println(1); } if (true && false) { System.out.println(2); } if (false && true) { System.out.println(3); } if (false && false) { System.out.println(4); } // 좌,우항이 모두 true인 조건문은 하나이므로1이 출력된다. if (id.equals("egoing") && password.equals("111111")) { System.out.println("right")..
조건문 - switch 조건문 switch 조건문의 또 다른 메소드로switch문의 경우 괄호의 숫자가 주어지면 해당 숫자의case들이 끝까지 실행된다.예를 들어 switch2 라면 case2 부터 존재하는 case 까지 전부 실행하게 된다 System.out.println("switch(1)"); switch(1){ case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: System.out.println("three"); } System.out.println("switch(2)"); switch(2){ case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: S..
조건문 - if 조건문의 문법 if 조건은 if문으로 시작한다. if문 뒤에 소괄호와 중괄호가 위치하며소괄호는 if절이라 부르며, 중괄호는 then절이라 부른다.if절은 true, false가 올수 있고, then절은 해당 조건을 만족할때 실행하는 구절이 이다. if(true){ System.out.println("result : true”);// true일경우 result : true를 출력한다. if(false){ System.out.println("result : true”);// false일경우 result : true를 출력한다. (아무것도 출력되지 않음) elseif문의 부가 기능으로 if-else절로 사용한다. if절의 값에 따라true일때는 if의 then절이 실행되며, false일때는 else의 then..