java 수업 DAY 6 // BlockScope, if else, 삼항연산자 ?

java 수업 DAY 6 // BlockScope, if else, 삼항연산자 ?

중괄호 - 범위를 나타낸다 , 여러개를 쓸 수 있다 , 짝이 맞아야한다 (중괄호 오류는 찾기 힘들다)

BlockScope - 중괄호 같은 것들

class TestBlockScope { public static void main(String[] args) { int b = 55; //지역 변수 - 범위 안에서 선언한 변수 (34567에서 사용가능) { int a = 10; // 지역 변수 (local variable) - 범위가 중요 - 들어 있는 범위에만 사용가능 } // - 456에서 사용가능 System.out.println(a); // a는 여기서 사용 불가 이미 중괄호로 끝을 맺어서 사라짐 - 오류남 } }

class TestBlockScope { public static void main(String[] args) { int b = 55; int a; { a = 10; // 밖에서 선언을 하면 사용 가능 } System.out.println(a); } }

제어문

조건문

형식 - if () { // ()에는 논리 값(true, false)이 들어온다

} - then 절

논리값이 true일시 - 중괄호 안으로 들어감

논리값이 false일시 - 중괄호를 건너뛴다

class Testif { public static void main(String[] args) { if (true) { // false 일시 world만 출력 System.out.println("Hello"); // then 절 } System.out.println("World"); } }

// 사용자가 입력한 정수를 출력하세요 (만약 음수를 입력하면 절대값으로 변경해주세요.) import java.util.Scanner; class PrintAbs { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("정수? "); int number = scan.nextInt(); if (number < 0) { number *= -1; } System.out.println(number); } }

// 사용자가 입력한 정수가 짝수일때 "짝수입니다"라고 출력해보기. // 홀수일때 "홀수입니다" 출력하기. import java.util.Scanner; class Even { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("정수: "); int number = scan.nextInt(); if (number % 2 == 0) { System.out.println("짝수입니다"); } if (number % 2 != 0) { System.out.println("홀수입니다"); } } }

if (조건식) { // 조건식이 true일때 then 절 실행

} else { //조건식이 false일 때 실행

}

import java.util.Scanner; class Even { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("정수: "); int number = scan.nextInt(); if (number % 2 == 0) { System.out.println("짝수입니다"); } else { System.out.println("홀수입니다"); } } }

// 상용자의 국영수 평균점수가 80점 이상이면 "합격" 출력 // 80 미만일 경우 "불합격"과 부족한 점수를 출력해보세요. import java.util.Scanner; class TestScore { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("국어: "); int kor = scan.nextInt(); System.out.print("영어: "); int eng = scan.nextInt(); System.out.print("수학: "); int math = scan.nextInt(); int avg = (kor + eng + math) / 3; if (avg >= 80) { System.out.println("합격"); } else { System.out.println("불합격"); System.out.println("부족한 점수: " + (80 - avg)); } } }

// 사용자에게 정수를 2개 입력받아 큰 수에서 작은 수를 나눈 몫과 나머지 출력 1. import java.util.Scanner; class DivideModRemainder { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("첫번째 정수: "); int first = scan.nextInt(); System.out.print("두번째 정수: "); int second = scan.nextInt(); if (first > second) { System.out.println("몫: " + (first / second)); System.out.println("나머지: " + (first % second)); } else { System.out.println("몫: " + (second / first)); System.out.println("나머지: " + (second % first)); } } }

// 2. import java.util.Scanner; class DivideMod { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("첫번째 정수: "); int first = scan.nextInt(); System.out.print("두번째 정수: "); int second = scan.nextInt(); if (first > second) { int temp = first; // c에 a를 집어 넣고 a엔 b를 집어넣고 b에는 c를 second = first; first = temp; } System.out.println("몫: " + (second / first)); System.out.println("나머지: " + (second % first)); } }

삼항연산자 ?

class TernaryOper { public static void main(String[] args) { // ? - 삼항연산자, 왼쪽 논리 값이 올 수 있음 왼쪽의 값 true면 클론 기준 왼쪽 false면 오른쪽 System.out.println((true) ? "Hello" : "World"); int num = (false) ? 10 : 20; System.out.println(num); if (true) { System.out.println("Hello"); // ? 와 같음 } else { System.out.println("World"); } } }

import java.util.Scanner; public class Metropolis { public static void main(String[] args) { boolean isCapital; int citizens; int riches; Scanner sc = new Scanner(System.in); System.out.print("수도입니까?(수도: 1 수도아님: 0)"); isCapital = (sc.nextInt() == 1) ? true : false; // ? ~~ 없어도 똑같음 System.out.print("인구(단위: 만) "); citizens = sc.nextInt(); System.out.print("부자의 수(단위: 만)"); riches = sc.nextInt(); boolean isMetro = (isCapital && citizens >= 100) || (riches >= 50); System.out.println("메트로폴리스 여부: " + isMetro); } }

// 콘솔 자판기 // 1번. 콜라 // 2번. 사이다 // 3번. 환타 import java.util.Scanner; class TestIF1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("1 콜라 2 사이다 3 환타 ? "); int number = scan.nextInt(); if (number == 1) { System.out.println("콜라"); } else if (number == 2) { // else 위에 있는 조건식이 거짓일때 System.out.println("사이다"); } else if (number == 3) { System.out.println("환타"); } else { System.out.println("잘못된 입력"); } } }

// 사용자에게 컵의 용량을 정수로 입력받아서 // 100미만은 small // 100이상 200미만은 mediu, // 200이상은 large 출력하기. 1.ver import java.util.Scanner; class Volume { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("컵의 용량을 입력하세요: "); int vol = scan.nextInt(); if (vol < 100) { System.out.println("small"); } else if (vol >= 100 && vol < 200) { System.out.println("medium"); } else if (vol >= 200) { System.out.println("large"); } } }

// 2.ver import java.util.Scanner; class Volume { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("컵의 용량을 입력하세요: "); int vol = scan.nextInt(); if (vol < 100) { System.out.println("small"); } else if (vol < 200) { // 이렇게 적어도 같은 값이 나온다 System.out.println("medium"); } else { System.out.println("large"); } } }

// 점수 // A 90점 이상 // B 80점 이상 90점 미만 // C 70점 이상 80점 미만 // D 60점 이상 70점 미만 // F 60점 미만 import java.util.Scanner; class Score { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("점수: "); int sco = scan.nextInt(); if (sco >= 90) { System.out.println("A"); } else if (sco >= 80) { System.out.println("B"); } else if (sco >= 70) { System.out.println("C"); } else if (sco >= 60) { System.out.println("D"); } else { System.out.println("F"); } } }

from http://hiapprendre.tistory.com/11 by ccl(A) rewrite - 2021-12-01 18:01:31