• 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

[03] Java_base

10 Jul 2020

Reading time ~2 minutes

Table of Contents
  • 목차
  • 사용자에게 입력값 받기
  • 연산자
    • 산술연산자
      • ++ 유의점
    • 비교 연산자
    • 비트 연산자
    • 비트쉬프트(shift) 연산자
    • 연산자 우선순위
  • 같이 풀어 볼 문제
    • [문제 1]
    • [문제 2]
    • [문제 3]
    • [문제 4]
    • [문제 5]
    • [문제 6]

목차

  • 사용자에게 입력값 받기
  • 연산자
    • 산술연산자
      • ++ 유의점
    • 비교 연산자
    • 비트 연산자
    • 비트쉬프트(shift) 연산자
    • 연산자 우선순위
  • 같이 풀어 볼 문제

사용자에게 입력값 받기

  1. Scanner import 하기
    import java.util.Scanner;
  2. Scanner class에 불러오기
    Scanner scan = new Scanner(System.in);
  3. 입력 받을 값 크기 잡아주기
    실수 입력값 double r = scan.nextDouble();
    정수 입력값 double r = scan.nextInt(); etc

[ex]

import java.util.Scanner;
public class hey {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	Scanner scan = new Scanner(System.in);
		
			double r = scan.nextDouble();
			double area = 3.141593*r*r;
			
			System.out.println(area) ;
                              Scan.close()
			}
}

연산자

산술연산자

• 복합 연산자 image

• 증가, 감소 연산자 image

++ 유의점

모두 같다

int a = 0;
		a+= 1;
		a= a+1;
		a++;
		++a: 

앞에 있으면 업데이트 후 문장 실행

int a = 0;
	    int b = ++a + 1;
	    System.out.println(a);
	    System.out.println(b);

(결과)
1
2

뒤에 있으면 문장실행후 업데이트

public class hey {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
	    int a = 0;
	    int b = a++ + 1;
	    System.out.println(a);
	    System.out.println(b);

(결과) 1
1

비교 연산자

연산의 결과로 true 혹은 false 를 반환함. image

비트 연산자

image

image

image

비트쉬프트(shift) 연산자

왼쪽으로의 비트 열 이동은 2의 배수의 곱 오른쪽으로의 비트 열 이동은 2의 배수의 나눗셈

image

image

연산자 우선순위

image


같이 풀어 볼 문제

[문제 1]

  1. Console 화면에 어떻게 출력될까요?
   public class HelloGBT {
	public static void main(String[] args) {
		int a = 7;
		int b = 2;
		double result = a/b;
		System.out.println(result);
	}
}

1.1 그럼 원하는 값을 어떻게 출력할까요??

public class HelloGBT {
public static void main(String[] args) {
int a = 7;
int b = 2;
double result = (double)a/(double)b;
System.out.println(result);
}

[문제 2]

연산자 출력_Console 화면에 어떻게 출력될까요?

public class HelloGBT {
	public static void main(String[] args) {
		int a = 7;
		int b = 3;
		System.out.println(++a);    
		System.out.println(b++);    
		System.out.println(--a);      
		System.out.println(b--);    
		System.out.println(a+=b);   
	}
}

(답) 화면출력 해당 값 8 a=8 3 b=4 7 a=7 4 b=4 10 a=10

[문제 3]

연산자 출력_Console 화면에 어떻게 출력될까요?

public class HelloClass {
	public static void main(String[] args) {
	// TODO Auto-generated method stub
		int a = 2;
		int b = 2;
		int c = a * (b++);
		System.out.println(c);
		System.out.println(a * (++b));
	}
}

[문제 4]

연산자 출력_Console 화면에 어떻게 출력될까요?

public static void main(String[] args) 
{
	int a;
	int b = 20;
	int c = 30;
	a = b + c;
	System.out.println(a);
	a = ++b + ++c;
	System.out.println(a);
	a = b++ + c++;
	System.out.println(a);
	a = b-- - --c;
	System.out.println(a);
}

[문제 5]

Console 화면에 어떻게 출력될까요?

public class hello {
	public static void main(String[] args) {
		int a = 5, b = 15;
		System.out.println( a>b);
		System.out.println(a<b);
		System.out.println(a==b);
		System.out.println(a!=b);
	}
}

[문제 6]

문제가 있는 코드다.
문제를 찾아라


public class GBTHello {

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

double choice = scan.nextInt();
switch(choice)
{
case 1: System.out.println("당신은 Java를 좋아한다");
	break;
case 2: System.out.println("당신은 Java를 사랑한다");
	break;
case 3: System.out.println("당신은 Java를 흠모한다");
default:
	System.out.println("모르겠음");
}

scan.close();

}

}

(답) double choice = scan.nextInt(); 여기서 앞은 더블인데 뒤는 인트로 받고있음



BasicJava Share Tweet +1