728x90
반응형
SMALL
  • 프로그램 언어에서 실행 순서를 변경하거나 조건에 따라 실행하거나 특정 부분은 반복하거나 할 경우 쓰는 구문
  • 프로그램은 일반적으로 위에서 아래로 순차적으로 실행
  • 제어문의 종류 조건문, 반복문, 기타 제어문 조건문 : if, switch 반복문 : while, for, do ~ while 기타 제어문 : break, continue

(1) 조건문

  • "조건에 따라 달라지는 것"을 구현하는 문장
1) if 문(Statement)
    1-1) if (조건) {
                          실행할 문장;
                      }
    1-2) if (조건) { 실행할 문장; }
          else if (조건) { 실행할 문장; }
          ....
          else { 실행할 문장; }

2) switch 문
    - switch ~ case
    switch(변수){
        case 값1 :
               실행할 문장;
               break;
        case 값2 :
               실행할 문장;
               break;
        default :
               실행할 문장;
    }

package Day02;

public class IfTest {

	public static void main(String[] args) {
	// TODO Auto-generated method stub
	p("If Test");
	p("*************");

	int a = 1;
	if( a > 0 ) {
		p("a is positive");

	}

}

public static void p(String str) {
	System.out.println(str);
	}

}

결과값
If Test

************

a is positive
package Day02;

public class IfTest {

	public static void main(String[] args) {
	// TODO Auto-generated method stub
	p("If Test");
	p("*************");
	int a = -1;
	if( a > 0 ) {
		p("a is positive");

	}

}

public static void p(String str) {
	System.out.println(str);
}

}

결과값
If Test

*************

- 1일때 값이 안나오고 그냥 넘어간다.
package Day02;

import java.util.Random;

public class IfTest {

	public static void main(String[] args) {
	//TODO Auto-generated method stub
	p("If test");
	p("*************");

	int a = 1;
	if(a > 0) {
		p("a is positive");
	}

	p("if-else");
	int b = -1;
	if(b > 0) {
		p("b is positive");
	}
	else {
		p("b is not positive");
	}

	p("if-else if ***");
	int c = 60;
	if(c > 50) {
		p("c is big number");
	}

	p("if-else if else");
	int d = -30;
	if(d > 0) {
		p("d is positive");
	}
	else if(d == 0) {
		p("d is zero");
	}
	else {
		p("d is negative");
	}

	// nested(중첩) if statement
	p("if-if *** ");
	int math = 90;
	int eng = 35;

	if(math >= 60) {
		if(eng >= 60) {
			p("Pass");
		}
		else
			p("Fail");

	}
	else
		p("Fail");

	// 조건을 && 등의 연산자로 묶을 수 있음
	if(math >= 60 && eng >= 60)
		p("pass");
	else
		p("Fail");
	// if 문은 {} 괄호 생략 가능

	p("***********");
	p("switch");
	p("***********");

	int i = 3;
	switch(i) {
		case 1 :
			p("1인가?");
		case 2 :
			p("2였네");
		case 3 :
			p("3맞지");
		default :
			p("다 아닌데?");
	}

	Random rd = new Random();
	int j = rd.nextInt(3);

	switch(j) {
		case 0:
			p("꽝");
			break;
		case 1:
			p("꽝");
			break;
		case 2:
			p("축!당첨!");
			break;

	}

}
public static void p(String str) {
	System.out.println(str);

}
728x90
반응형
LIST

'개발 > JAVA' 카테고리의 다른 글

Colors (추후 수정 필요 !)  (0) 2023.01.09
배열 (Array)  (0) 2023.01.09
Random (임의의 수를 얻는 도구)  (0) 2023.01.09
User Input (사용자로부터 입력을 받는 기능)  (0) 2023.01.09
연산(Operation), 연산자(Operator)  (0) 2023.01.09
728x90
반응형
SMALL
  • 사용방법
    1. java.util.Random을 import
    2. Random rd = new Random();
    3. 다양한 방식으로 사용한다. rd.nextInt() : -2147483648 ~ 2147483647 중 하나 선택 rd.nextInt(숫자) : 0부터 숫자-1 까지의 수 중 하나(정수)를 얻음 rd.nextFloat(), rd.nextDouble() : 0부터 1사이의 실수 를 얻음

 

package Day02;

import java.util.Random;

public class IfTest {

	public static void main(String[] args) {
	//TODO Auto-generated method stub
	p("If test");
	p("*************");

	int a = 1;
	if(a > 0) {
		p("a is positive");
	}

	p("if-else");
	int b = -1;
	if(b > 0) {
		p("b is positive");
	}
	else {
		p("b is not positive");
	}

	p("if-else if ***");
	int c = 60;
	if(c > 50) {
		p("c is big number");
	}

	p("if-else if else");
	int d = -30;
	if(d > 0) {
		p("d is positive");
	}
	else if(d == 0) {
		p("d is zero");
	}
	else {
		p("d is negative");
	}

	// nested(중첩) if statement
	p("if-if *** ");
	int math = 90;
	int eng = 35;

	if(math >= 60) {
		if(eng >= 60) {
			p("Pass");
		}
		else
			p("Fail");

	}
	else
		p("Fail");

	// 조건을 && 등의 연산자로 묶을 수 있음
	if(math >= 60 && eng >= 60)
		p("pass");
	else
		p("Fail");
	// if 문은 {} 괄호 생략 가능

	p("***********");
	p("switch");
	p("***********");

	int i = 3;
	switch(i) {
		case 1 :
			p("1인가?");
		case 2 :
			p("2였네");
		case 3 :
			p("3맞지");
		default :
			p("다 아닌데?");
	}

	Random rd = new Random();
	int j = rd.nextInt(3);

	switch(j) {
		case 0:
			p("꽝");
			break;
		case 1:
			p("꽝");
			break;
		case 2:
			p("축!당첨!");
			break;

	}

}
public static void p(String str) {
	System.out.println(str);

}
728x90
반응형
LIST

'개발 > JAVA' 카테고리의 다른 글

배열 (Array)  (0) 2023.01.09
제어문(Control Statement)  (0) 2023.01.09
User Input (사용자로부터 입력을 받는 기능)  (0) 2023.01.09
연산(Operation), 연산자(Operator)  (0) 2023.01.09
class first  (0) 2023.01.09
728x90
반응형
SMALL

(1) 실행을 시작할 때 받는 방법 java 클래스명 입력1 입력2 입력3 입력4 ...... 으로 실행하고 args[0], args[1]........ 으로 사용 - 야간 배치 작업(정해진 시간이나 조건에 동작하는 작업)에 주로 쓰임

(2) Scanner 이용 아무 때나 입력이 필요할 때 사용함

  • 사용방법
  1. java.util.Scanner를 import
  2. Scanner sc = new Scanner(System.in);
  3. 다양한 방법으로 입력을 받는다. sc.next() : 문자 sc.nextInt() : 정수 sc.nextFloat(), sc.nextDouble() : 실수

//#은 숫자를 의미 넘버~ Arguments는 함수 호출을 할때 넣는 값 //Arguments, Value, Parmeter 똑같이 호출하는 것이지만 다르게 쓰임 //length : 증가하게 만드는 것

728x90
반응형
LIST

'개발 > JAVA' 카테고리의 다른 글

제어문(Control Statement)  (0) 2023.01.09
Random (임의의 수를 얻는 도구)  (0) 2023.01.09
연산(Operation), 연산자(Operator)  (0) 2023.01.09
class first  (0) 2023.01.09
변수(Variable)  (0) 2023.01.09

+ Recent posts