java 수업 DAY 5 // 형변환, boolean, 논리 연산자, String

java 수업 DAY 5 // 형변환, boolean, 논리 연산자, String

형변환

class Casting { public static void main(String[] args) { double d = 58; // 자동 형변환 (auto casting) - 작은 것에서 큰 것으로 갈때 -> down casting System.out.println(d); int i = 55.55; // 에러 남 (자료의 손실이 발생) .55를 버리고 컴파일러에게 알려줘야함 int i = (int) 55.55; // 명시적인 형변환 -> up casting // (이유는 피라미드 구조로 작은것(위)에서 큰것(아래)로) System.out.println(i); } }

public class TypeConversion { public static void main(String[] args) { int i; double f; f = 5 / 4; // 1.0 자동 형변환 - 정수 정수 계산 -> 정수 -> double로 실수로 변환 System.out.println(f); f = (double) 5 / 4; // 1.25 형변환 5.0 /4 큰쪽을 따라감 System.out.println(f); f = 5 / (double) 4; // 1.25 System.out.println(f); f = (double) 5 / (double) 4; // 1.25 System.out.println(f); i = (int) 1.3 + (int) 1.8; // 2 형변화 1.3에서 0.3을 삭제 System.out.println(i); } }

// double d = 54.123; 변수를 정수 부분 실수 54 . 0.123 class PrintNumber { public static void main(String[] args) { double d = 54.123; int i =(int)d; System.out.println("정수: " + i); System.out.println("실수: " + (d - i)); } }

boolean = 논리값 - true, false 참 or 거짓 하나만 가질 수 있는 타입

< - 크냐? 물어보는 것

public class TestBoolean { public static void main(String[] args) { boolean t = true; // (부울, 불리언, 불련) boolean f = false; System.out.println(t); System.out.println(f); } }

class TestBoolean2 { public static void main(String[] args) { System.out.println(10 < 5); // false 출력 System.out.println(10 > 5); // true 출력 } }

// 사용자가 입력한 정수가 0보다 크면 true를 출력 import java.util.Scanner; class Testboolean3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("정수: "); int number = sc.nextInt(); boolean result = number > 0; System.out.println(result); } }

class TestBoolean4 { public static void main(String[] args) { System.out.println(10 == 10); // 관계 연산자 왼쪽 값 = 오른쪽 값이 같냐? System.out.println(10 != 10); // ! 부정을 뜻한다 - 다르냐? 라고 물어봄 (!가 먼저) System.out.println(10 >= 11); // >= 크거나 같나 <= 작거나 같나 System.out.println(10 <= 11); } }

// 1. 사용자가 입력한 정수 두 개의 합이 0이면 true 출력 import java.util.Scanner; class SumZero { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("정수1: "); int x = sc.nextInt(); System.out.print("정수2: "); int y = sc.nextInt(); int sum = x + y; System.out.println("합은: " + (x + y)); boolean result = (sum == 0); System.out.println(result); } }

// 2. 사용자가 입력한 정수가 짝수이면 true 출력 import java.util.Scanner; class Even { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("정수: "); int x = sc.nextInt(); System.out.println(x % 2 == 0); } }

// 오류 class TestBoolean5 { public static void main (String[] args) { int a = 5; System.out.println(0 <= a <= 10); // 오류나는 이유는 0 <= a 먼저 실행 // true값 true <= 10이 됨 } }

class TestBoolean5 { public static void main (String[] args) { int a = 5; System.out.println(0 <= a && a <= 10); System.out.println(true && true); System.out.println(true && false); System.out.println(false && true); System.out.println(false && false); System.out.println(true || true); System.out.println(true || false); System.out.println(false || true); System.out.println(false || false); } }

논리연산자 and o and o = o / x and x = x / o and x = x and연산자 &

or o or o = o / x or x = x / o or x = o or 연산자 || (shift \)

class TestBoolean6 { public static void main(String[] args) { // a가 5의 배수이거나 10의 배수입니까? int a = 15; System.out.println(a % 5 == 0 || a % 10 == 0); // b가 0보다 크면서 짝수 입니까? int b = 55; System.out.println(b > 0 && b % 2 == 0); } }

// 사용자가 3대(벤치, 데드, 스쿼트) 측정을 한 값을 입력합니다. // 합이 500이상이면 true라고 출력해보세요. import java.util.Scanner; class Exercise { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("벤치: "); int bench = scan.nextInt(); System.out.print("데드: "); int dead = scan.nextInt(); System.out.print("스쿼트: "); int squat = scan.nextInt(); int sum = bench + dead + squat; System.out.println(sum >= 500); } }

// 사용자가 입력한 정수가 // 0보다 크고 7의 배수일 때 true 출력 import java.util.Scanner; class Number { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("정수? "); int num = scan.nextInt(); System.out.println(num > 0 && num % 7 == 0); } }

not 연산자 - 반대로 출력 true면 false로 false면 true로 !@@@

☆정리★

type

기초, 기본, 원시형 (primitive type)

정수형 : int, long

실수형 : double

문자형 : char

논리형 : boolean

연산자

산술연산자 : +, -, *, /, %

대입연산자 : =, +=, -=, *=, /=, %=

증감연산자 : ++, --

관계연산자 : >, <, ==, <=, >=

논리연산자 : &&, ||, !

// 상수 public class TestConst { public static void main(String[] args) { int myNumber = 10; // 10 상수 myNumber은 변수 final int MY_NUMBER2 = 10; // 변경할 수 없음 상수, 상수표현은 대문자로, 단어 연결 _ myNumber2 = 55; final double PI = 3.14; } }

TestConst.java:7: error: cannot assign a value to final variable myNumber2

myNumber2 = 55;

^

1 error

String

class TestString { public static void main(String[] args) { System.out.println("문자열문자열"); String str = "테스트테스트"; // 스트링 (대문자로 시작) System.out.println(str); } }

import java.util.Scanner; public class InputString { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("이름을 입력하세요."); String name = scan.nextLine(); // 문자열 입력 nextLine() System.out.println(name); System.out.print("나이를 입력하세요?"); int age = scan.nextInt(); System.out.println(age); } }

/* 이름 -> 나이 는 잘 됨

나이 -> 이름은 오류가 남

(숫자 입력 후 엔터 / 숫자만 빼옴->엔터가 남아있음 / 다음 이름 작업 할때 비어있는 빈칸을 가져옴)

이경우 nextInt~~~ 한후 바로 scan.nextLine();을 넣고 이름을 입력하세요~~~ 하면 됨 */

import java.util.Scanner; public class InputString { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("나이를 입력하세요?"); int age = scan.nextInt(); System.out.println(age); scan.nextLine(); System.out.print("이름을 입력하세요."); String name = scan.nextLine(); // 문자열 입력 nextLine() System.out.println(name); } }

문자열의 크기는 정해지지 않음

합연산시 '문자열 + a '이면 a를 문자열 취급한다

char a = "a"; - 문자

String s = s 문자열 문자열 - 문자들이 모여있는 집단

class StringEqual { public static void main(String[] args) { String abc = "abc"; String another = "abc"; System.out.println(abc == another); // wrong System.out.println(abc.equals(another)); // 문자열이 같고 다름을 비교할땐 equals System.out.println(abc.length()); // 문자의 개수 System.out.println(abc.charAt(0)); // charAt 가로 안의 숫자 - 순서 - 0부터 센다 //0-a 1-b 2=c } }

from http://hiapprendre.tistory.com/9 by ccl(A) rewrite - 2021-12-01 00:27:20