자바 학습 & 복습 3일차

728x90
import java.util.Scanner;

public class T_22_1_10 {

	public static void main(String[] args) {
		
		double d = 3.141592;
		System.out.println(Math.round(d)); // 값은 바로 3
		
		double dd = Math.round(d*1000)/1000.0;//.0을 미리 붙여서 double로 만들기,
        //int끼리 나누는 것이므로 3이 대신 출력됨
		
        System.out.println(dd); // 미리 3142로 반올림 후에 1000.0을 다시 나눠서 3.142로 변환
		
		int i = 10, ii = 8;
		float iii = (float)i/ii;
		int iiii = i%ii; //  %나머지 연산자
		System.out.printf("%d를 %d로 나누면 %f이 됩니다%n", i, ii, iii);
		System.out.printf("%d를 %d로 나누고 남은 값은 %d 입니다%n", i, ii, iiii);
		// 나머지 연산자는 부호 무시됨
		
        System.out.println(i % -ii); //2로 동일
		
// 비교 연산자		
		
		// 두 연산자를 비교하여 True False를 반환
		// ==는 변수의 값을 비교하는 연산자
		String str = "abc";
		String str2 = "Abc";
		boolean result = str.equals("abc"); // 문자 비교할때는 == 말고 .equals() method 사용
		System.out.println(result);
		System.out.println(str2.equalsIgnoreCase(str));
        // 만약 대소문자 구분없이 비교원하면 equalIgnoreCase method 사용
		
		
// 논리 연산자
		int x = 10, y = 15;
		System.out.println( x > 11 || y >10); // || or의 의미
		System.out.println( x%2==0 || x%3==0); //x는 2의 배수 or 3의 배수이다
		System.out.println( x > 11 && y > 10); // && and의 의미
		
		int ch = 11;
        // 유니코드상 '0'~'9'는 48~57 따라서 ch한테 5라는 숫자를 집어넣으면
        //자동으로 53으로 변환되어 True식이 나옴
		System.out.println('0' < 'ch' && 'ch' <= '11');
		
		//예시문제 - Scanner를 이용해 문자입력을 하나 하고 영문자인지 소문자인지 판별
		
		Scanner scanner = new Scanner(System.in);
		
		char ch = ' ';
		System.out.println("아무 문자 하나 입력");
		String input = scanner.nextLine();
		ch=input.charAt(0);
		//char은 한가지 문자열만 받을 수 있지만 상식적으로 사람들이 한가지만
        //입력할일이 없으므로 charAt() method를 사용해서 한가지 문자열만 추출.
        //안의 숫자 0의 의미는 주어진 문장에서 첫번째 str만 추출
		// 즉 "안녕하세요" = "안"만 추출됨
        
		if('0'<=ch && ch<='9') {
			System.out.println("숫자입니다");
		}
		
		if('a'<=ch && ch<='z') {
			System.out.println("영소문자입니다");
		}
		if('A'<=ch && ch<='Z') {
			System.out.println("영대문자입니다");
		}	
        
        
        //논리 부정 연산자: !
        
		boolean b = true;
		System.out.println(!b); //false 값 출력
		System.out.println(!!b); //!! 부정 연산자값이 2번 입력되었기 떄문에 false > true 출력
		
		//예시 - ch가 소문자가 아닐때 true출력
		char ch = 'A';
		System.out.println(!('a'<=ch && ch <= 'z'));
		
// 조건연산자: ?
		int x = 4, y=5;
		int result = (x>y)? x:y; // if 문을 가볍게 쓰기위해 만들어진 조건 연산자
		System.out.println(result); // x가 y보다 작으니 y가 출력이 되었고
        //만약 x가 더 크다면 x가 대신 출력이 됨

		//위의 식을 if문으로 풀어보면
		
		if(x>y) {
			System.out.println(x);
		}
		else {
			System.out.println(y);
		}
		

// 대입연산자
		System.out.println(x = 10);
	}
}
728x90

'Java > 자바의정석 기초편' 카테고리의 다른 글

자바 학습 & 복습 6일차 - array 배열  (0) 2022.01.14
자바 학습 & 복습 5일차  (0) 2022.01.13
자바 학습 & 복습 4일차  (0) 2022.01.12
자바 학습 & 복습 2일차  (0) 2022.01.10
Java 학습 & 복습 1일차  (0) 2022.01.10