• Home
  • About
    • Yerim Oh photo

      Yerim Oh

      Happy and worthwhile day by day :)

    • Learn More
    • Email
    • LinkedIn
    • Instagram
    • Github
    • Youtube
  • Posts
    • All Posts
    • All Tags
  • Projects

[04] Java_base

09 Jul 2020

Reading time ~2 minutes

Table of Contents
  • 함수 목차
  • 함수
  • if 연산자
    • 복잡한 연산
  • switch 연산자
  • for문
  • while 문
    • 문제1
    • Do-while 문
  • 같이 풀어볼 문제
    • [문제 1]
    • [문제 2]
    • [문제 3]

함수 목차

  • if 연산자
    • 복잡한 연산
  • switch 연산자
  • for문
  • while 문
    • 문제1
    • Do-while 문
  • 같이 풀어볼 문제

함수

if 연산자

• 기본틀

public class hello {

	public static void main(String[] args) {
    if(i>100){
      print(i+"is big"); 
			}
    else if(0<i<=100){
      print(i+"is little big");
    }
    else{
      print(i+"is small");
    }
}

image

[주의] if 조건 (괄호 안) 의 문장은 BooleanType의 결과만!

복잡한 연산

:논리연산자 사용 중요!!
image


switch 연산자

정수형만 조건입력가능

int i = 어떤 값;   
switch(i) {                    //변수 i 값에 따라 프로그램의 흐름 전환           
    case 0: 처리할 명령 A;      // i == 0 일때 명령 A를 실행
		break;                     // 여기멈춘다
	  case 1: 처리할 명령 B;
		break;
	  case 2: 처리할 명령 C;
		break;
	  default: 처리할 명령 D;     //eles와 비슷
			break;
            }

[Warning!] • break를 하지 않으면 다음 조건까지 실행이 됨. • switch 안에 들어갈 수 있는 값은 int, char, 혹은 String임. • default는 없어도 상관 없음. • Default 없이 case에 없는 조건 실행하면 아무 일X


for문

: 일정한 수만큼 특정 명령어를 반복할 때 사용 image

       for(int i = 0;i<4;i++)
        {

        }

while 문

image

문제1

문제가 있는 코드임 무슨문제가 있을까?

while (n < 10)
		{
		      if(n == 2)
		         continue;
		      System.out.println(n++);
		}
		System.out.println(“loop finished”);
  

(답)

  • 만약 n 가 2이라면 continue 문장이 실행된다.
    -continue 문장이 실행되면 System.out.println(n++)는 실행되지 않고 다시 반복문의 처음으로 돌아간다 (n값은 유지)
  • 0과 1만 반복하여 while문이 끝나지 않는다
  • n 이 10이면 반복문이 끝나고 “loop finished” 라고 화면 출력한다.

Do-while 문

while 문은 조건을 만족하면 특정 문장을 실행한다.
조건이 만족하지 않아도 최소 한번은 실행 image


같이 풀어볼 문제

[문제 1]

주어진 숫자까지의 합을 계산하는 프로그램을 개발해라
힌트) for문

(답)

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);

int count = scan.nextInt();
int total = 0;

for(int i = 1; i<=count ;i++)
{
	total += i;
}

System.out.println(total);

scan.close();

[문제 2]

0이 나올때 까지 계속 숫자 입력 받아서 평균 계산하는 프로그램을 개발해라
힌트) while문

(답)

Scanner scan = new Scanner(System.in);
int num = -1;
double total = 0;
int count = 0;

while(true)
{
num = scan.nextInt();
if(num == 0)
	break;
total += num;
count++;
}
System.out.println(total / count);
scan.close();

[문제 3]

사용자로부터 크기를 입력받아 다음과같이 출력하는 프로그램을 개발해라
힌트) for문

1) image

(답)


int row = scan.nextInt();
int col(->) = scan.nextInt();

for(int i = 0;i<row;i++)
{
for(int j = 0;j<col;j++)
{
	System.out.print("*");
}
System.out.println();
}

scan.close();


2)
image

(답)

 for(int i = 1; i<=a;i++ ) {
			for(int j = 2; j<=i;j++ ){
								System.out.print("*");
	 		}
			System.out.println("*");
		}

3) image

(답)

			
      		for(int i = 1; i<=a;i++ ) {
			for(int j = i+1; j<=a;j++ ){
				System.out.print("*");
			}
			System.out.println("*");
		}



BasicJava Share Tweet +1