Posts 자바-1. 표준 입출력 클래스
Post
Cancel

자바-1. 표준 입출력 클래스

표준입출력

println() : 출력후엔터 메소드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void main(String[] args) {	
  //출력예
  //첫 번째 프로그램 입니다.
  //줄을 바꾼어 출력합니다.
  //여기까지 출력하고
  //줄을 바꿉니다.
  int age = 10;
  System.out.println("첫 번째 프로그램 입니다.");
  System.out.println("줄을 바꾸어 출력합니다.");
  System.out.println("여기까지 출력하고");
  System.out.println("줄을 바꿉니다.");
  System.out.println("줄을 바꿉니다.");
  System.out.println("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ");
}

printf() : 출력 메소드

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(String[] args) {
  //지시자
  /*
   * %d  : 10진수 정수 표현
   * %f : 실수
   * %c : 문자
   * %s : 문자열
   */
  //출력예 : 나의 나이는 10살입니다.
  System.out.printf("나의 나이는 %d살 입니다.", age);
  //출력예 : 나의 나이는 10살이고 10년 뒤에는 20살이 됩니다.
  System.out.printf("\n나의 나이는 %d살이고 %d년 뒤에는 %d살이 됩니다.", age, age, age+10);
}

지시자 (%d)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void main(String[] args) {
  // 연산자
  // 출력예 : 7 + 5 = 12
  /*
   * 7 + 5 =12 7 - 5 =2 7 * 5 = 35 7 / 5 = 1 7 % 5 = 2
   */

  int a = 7, b = 5;

  System.out.printf("%d + %d = %d\n", a, b, a + b);
  System.out.printf("%d - %d = %d\n", a, b, a - b);
  System.out.printf("%d * %d = %d\n", a, b, a * b);
  System.out.printf("%d / %d = %d\n", a, b, a / b);
  System.out.printf("%d %% %d = %d\n", a, b, a % b);
  System.out.printf("%d %% %d = %d\n", a, b, a % b); // 여기서 %%인경우는 지시자->문자열로 보여준다는의미
}

증감연산자

증감연산자 (전치,후치)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {
  // 증감연산
  /*
   * 출력예) 최소값 A = 10, B=10
   * 
   * a++ = 10, ++b = 11 실행후 a=11, b=11
   * a-- = 11, --b = 10 실행후 a=10, b=10
   **/

  int A = 10, B=10;

  System.out.printf("최소값 A = %d, B = %d\n", A, B);
  System.out.printf("A++ = %d, ++B = %d\n", A++, ++B); //후치증가연산자, 전치증가연산자
  System.out.printf("실행후 A = %d, B = %d\n", A, B);
  System.out.println();
  System.out.printf("A-- = %d, --B = %d\n", A--, --B);
  System.out.printf("실행후 A = %d, B = %d\n", A, B);
}

증감연산자 (전치,후치) 2

1
2
3
4
5
6
7
8
9
10
11
12
public static void main(String[] args) {
  int c = 5, d=6, e=0;
  System.out.println();
  // 첫번째 틀린경우 (이경우는 전치후치의 사용법을 모르는경우이다.)
  //c++; 
  //--d;
  //e=c+d;
  //System.out.printf("c = %d, d = %d, e = %d", c, d, e );

  e=++c + d--;
  System.out.printf("c = %d, d = %d, e = %d", c, d, e );
}

비교연산자

비교연산자 (==, !=)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static void main(String[] args) {
		/*
		 * 비교연산자 ==, !=
		 * 정수 3(a, b, c)개를 입력받아 a와 b, c를 각각비교하여 같으면 1,
		 * 같지않으면 0을 출력하고, 다음에는 같지 않으면 1, 같으면 0 출력
		 * 출력하고, 다음에는 같지 않으면 1, 같으면 0을 출력
		 * 입력예 ) 10, 20, 20
		 * 출력예 ) 0, 1, 1, 0
		 */ 
		
		Scanner scn = new Scanner(System.in);
		boolean result1, result2, result3, result4;
		System.out.printf("정수 3개를 입력하세요:");
		
		int a = scn.nextInt();  // 정수로 입력받을때 사용하는 
		int b = scn.nextInt();
		int c = scn.nextInt();
		
		result1 = (a == b);
		result2 = (b == c);
		result3 = (a != c);
		result4 = (b != c);
		
		System.out.printf("%b %b %b %b \n",result1, result2, result3, result4);
}	
//		System.out.printf("%b %b %b %b \n",(a == b), (b == c), (a != c), (b != c)); 이렇게도 사용가능하다

세개의 정수 비교하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args)
{
  /* 
   * 3개의 정수 a,b,c를 입력받아서 a가b보다 큰지, b가 c보다 
   * 크거나 같은지, a가 b보다 작거나 같은지, b가 c보다 작은지
   * 비교하여 참이면 true, 거짓이면 false을 각각 출력
   * 입력예 ) 1 2 2 
   * 출력예 ) false true true false  
   * */
  System.out.println("세개의 정수를 입력하세요");
  a = scn.nextInt();
  b = scn.nextInt();
  c = scn.nextInt();

  result1 = a>b;
  result2 = b>=c;
  result3 = a<=b;
  result4 = b<c;

  System.out.printf("%b %b %b %b \n",result1, result2, result3, result4);
 }

비교연산자 (boolean 자료형)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void main(String[] args)
{
  Scanner scn = new Scanner(System.in);

  System.out.println("정수 3개를 입력하세요");
  int a = scn.nextInt();
  int b = scn.nextInt();
  int c = scn.nextInt();

  boolean result1 = a==b;
  boolean result2 = a!=b;
  boolean result3 = a==c;
  boolean result4 = b==c;

  System.out.printf("%b, %b, %b, %b", result1,result2,result3,result4 );
}
This post is licensed under CC BY 4.0 by the author.