자바 2일차

달달한스위츠·2024년 1월 31일

자바배우기

목록 보기
2/43

오늘의 코드

package edu.java.variable08;

import java.util.Scanner;

public class ScannerMain01 {

	public static void main(String[] args) {
		System.out.println("변수 입력");
		
		// 입력을 받기 위한 변수는 Scanner 선언
		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수를 입력하세요: ");
		int userInput = sc.nextInt(); // 정수 입력
		System.out.println("입력받은 정수는 " + userInput + "입니다.");
		
		// nextInt() : 정수를 입력받는 메소드
		// nextDouble() : 실수를 입력받는 메소드
		// nextLine() : 문자열 한 줄을 입력받는 메소드
		// next() : 문자열 한 줄을 입력 받는 메소드, 엔터 버퍼를 처리
		
		System.out.println();
		System.out.println("실수 입력 : ");
		double x = sc.nextDouble();
		System.out.println("x = " + x);
		
		
		
	} // end main()

} // end ScannerMain01
package edu.java.variable09;

import java.util.Scanner;

public class ScannerMain02 {

	public static void main(String[] args) {
		// Scanner 사용시 주의할 점
		Scanner sc = new Scanner(System.in);
		
		System.out.println("나이 입력 : ");
		int age = sc.nextInt();
		System.out.println("age = " + age);
		
		System.out.println("이름 입력 : ");
		String name = sc.next();
		System.out.println("name = " + name) ;
		
		sc.close();
		

	} // end main()

} // end ScannerMain02
package edu.java.variable10;

import java.util.Scanner;

public class ScannerMain03 {

	public static void main(String[] args) {
		System.out.println("문자 하나 입력");
		
		Scanner sc = new Scanner(System.in);
		
		char ch1 = sc.next().charAt(0); // 문자 하나 입력 후 저장
		System.out.println("ch1 = " + ch1);
		
		
		sc.close();

	} // end main()

} // end ScannerMain03

package edu.java.oper01;

public class OperatorMain01 {

	public static void main(String[] args) {
		System.out.println("대입 연산다(=)");
		// 변수 = 값;
		// - 오른쪽의 값을 왼쪽의 변수에 저장하는 연산자
		int num1 = 246 + 234 + 100 / 10 ;
		System.out.println("nem1 = " + num1);
		
		System.out.println("==========");
		System.out.println("산술 연산자(+, -, *, /, %)");
		
		int num2 = 1 + 2;
		System.out.println(num2);
		num2 = 3 - 1;
		System.out.println(num2);
		num2 = 4 * 10;
		System.out.println(num2);
		num2 = 4 / 1; // 몫
		System.out.println(num2);
		num2 = 10 % 2; // 나머지
		System.out.println(num2);
		
        // (정수) / (정수) : 나눈 몫을 계산
		// (실수) / (실수), (실수) / (정수), (정수) / (실수) :
		//            소수점까지 계산하는 나눗셈
		
        System.out.println("정수 나눗셈 몫 : " + (246 / 100));
		System.out.println("실수 나눗셈 몫 : " + (246 / 100.0));
		System.out.println("정수 나눗셈 나머지 : " + (246 % 100));
		
        // 소수점 n째 자리까지 출력
		System.out.printf("소수점 6자리 출력 : %.6f", (246/100.0));
		

	} // end main()

} // end OperatorMain01


package edu.java.oper02;

// 복합 대입 연산자
// - (변수) = (변수) + 1
//   오른쪽 변수의 값에 1을 더하고 왼쪽 변수에 저장
// => 변수 += 1
//   코드의 길이를 단축시키기 위해 사용
public class OperatorMain02 {

	public static void main(String[] args) {

		int age = 16;
		age = age +1;
		System.out.println("age = " + age);
		
		age += 1;
		System.out.println("age = " + age);
		
		int x = 123;
		x -= 10; // x = x -10;
		System.out.println("x = " + x);
		
		
	} // end main()

} // end OperatorMain02
package edu.java.oper03;

// 증감 연산자
// - 변수의 값을 1 증가 또는 감소할 때 사용하는 연산자
// - 변수의 앞(prefix)와 뒤(suffix)에 사용
// - ++, --
public class OperatorMain03 {

	public static void main(String[] args) {
		System.out.println("증감 연산자(++, --)");
		
		int num1 = 100;
		num1++;
		// num1 += 1;
		// num1 = num1 + 1;
		System.out.println("num1 = " + num1);
		
		int num2 = 100;
		++num2;
		System.out.println("num2 = " + num2);
		
		
		int num3 = 100;
		int result = ++num3 + 5;
		System.out.println("num3 = " + num3);
		System.out.println("result = " + result);
		
		int num4 = 100;
		result = num4++ + 5;
		// num4(100) + 5가 먼저 실행되고, result에 값을 저장
		// num4를 1 증가시켜서 num4 =101이 저장됨
		System.out.println("num4 = " + num4);
		System.out.println("result = " + result);
		
		result = 0;
		
		int x = 10;
		result = x++ + 5 + ++x;
		// 퀴즈) result의 값과 그 이유 설명하기
		// 힌트) 연산 구조는 순차적으로 이루어 진다.
		// 예) 1 + 2 + 3 => (1 + 2 = 3) + 3 = 6
		
		// 계산 순서
		// 1. x++ + 5
		//        (1) x +5 ==> 10 + 5 ==> 15
		//        (2) x 증사 ==> x =11
		// 2. 15 + ++x
		//        (1) x 증가 ==> x = 12
		//        (2) 15 + 12 = 27
		// 3. result = 27
		System.out.println("result = " + result);
		
		
		
		
		
		
	} // end main()

} // end OperatorMain03
package edu.java.oper04;

public class OperatorMain04 {

	public static void main(String[] args) {
		System.out.println("비교 연산자");
		
		System.out.println(10 > 20); // 10이 20보다 큰가?
		System.out.println(10 < 20); // 10이 20보다 작은가?
		System.out.println(123 == 100); // 123이 100과 같은가?
		System.out.println(123 != 100); // 123이 100보다 다른가?

		System.out.println("논리 연산자");
		boolean A = true; // 1
		boolean B = false; // 0
		System.out.println(A && B); // 곱연산자(and)
		System.out.println(A || B); // 합연산자(or)
		System.out.println(!B); // 부정연산자(not)

		int age = 20;
		String sex = "남";
		
		// 나이가 19세 이상이고, 성별이 "남"인 경우
		System.out.println((age >= 19) && (sex == "남"));
		System.out.println((10 > 0) && (10 < 100));
		System.out.println((10 > 0) || (10 < 100));
		System.out.println((10 <= 0) || (10 <= 100));
		System.out.println(!(111 < 100));
		
		
		
		
		
	} // end main()

} // end OperatorMain04

**아래는 굳이 사용하지는 않는다 다만 알아는 둘것

이렇게 코드짜면 욕먹음**

package edu.java.oper05;

// SCE (Short-circuit evaluation : 짧은 계산)
// Lazy Evaluation (게으른 계산)
// - A && B 를 계산할 때 A가 false이면, B를 계산하지 않음
// - A || B 를 계산할 때 A가 true이면, B를 계산하지 않음
public class OperatorMain05 {

	public static void main(String[] args) {
		System.out.println("계산 규칙 확인");
		int x = 0;
		int y = 0;
		
		boolean b1 = (x += 10) < 0 && (y += 10) > 0;
		System.out.println(b1);
		
		// x += 10 의 연산 결과 : x = 10
		// 10 < 0 의 연산 결과 : false
		// 앞 쪽 연산이 false이므로 y += 10은 계산되지 않음
		System.out.println("x = " + x);
		System.out.println("y = " + y);
		
	} // end main()

} // end OperatorMain05

다시 아래부턴 코드시작

package edu.java.oper06;

import java.util.Scanner;

public class ScoreMain {

	public static void main(String[] args) {
		System.out.println("총점 및 평균 계산 프로그램");
		// 1. 입력받을 준비 : Scanner 생성
		
		// 2. 국어, 영어, 수학 점수를 정수로 입력잗아서 변수에 저장
		
		// 3. 국어, 영어, 수학 점수를 출력
		
		// 4. 총점을 계산하여 출력
		
		// 5. 평균을 계산하여 출력(소숫점 셋째자리까지)
		int tot = 0;
		
		Scanner sc = new Scanner(System.in);
		System.out.println("국어점수를 입력해주세요 : ");
		int kor = sc.nextInt();
		System.out.println("영어점수를 입력해주세요 : ");
		int eng = sc.nextInt();
		System.out.println("수학점수를 입력해주세요 : ");
		int met = sc.nextInt();
		
		tot = (kor + eng + met);
		
		System.out.println("국어점수입니다 : " + kor);
		System.out.println("영어점수입니다 : " + eng);
		System.out.println("수학점수입니다 : " + met);
		System.out.println("총점입니다 : " + tot);
		System.out.printf("평균입니다 : %.3f", (tot / 3.0));
		

		sc.close();
		
	} // end main()

} // end ScoreMain

여기서 부턴 if문

package edu.java.if01;

import java.util.Scanner;

// 흐름 제어(Flow Control)
// - 프로그램의 실행 흐름을 개발자가 원하는 방향으로 바꿀 수 있음
// - 제어문이라고 부름
// - 제어문에는 조건문과 반복문이 존재
// - 조건문 : if, if-else, if-else if-else, switch
// - 반복문 : for, while, do-while

public class IfMain01 {

	public static void main(String[] args) {
		System.out.println("if문");
		
		// if(조건) { 본문 }
		// - 조건이 참(true)이면 본문 실행
		
		Scanner sc = new Scanner(System.in);
		int score = sc.nextInt();
		
		// 만약 score가 90점 이상이면 A라고 출력해라!
		if (score >= 90) {
			System.out.println("A");
		}
		
		// 만약 score가 90점 미만이면 B라고 출력해라!
		if (score < 90) {
			System.out.println("B");
		}
		
		sc.close();
		
		
	} // end main()

} // end IfMain01
package edu.java.if02;

import java.util.Scanner;

public class IfMain02 {

	public static void main(String[] args) {
		System.out.println("if-else 문");
		// if(조건) { 본문A }
		// else { 본문B }
		// - 조건이 참이면 본문A가 실행되고 거짓이면 본문B가 실행
		
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
		
		
		if (x > 0) { // 만약 x가 0보다 크면 "양수"
			System.out.println("양수");
		} else { // 그게 아니면(x <= 0) "0보다 크지 않음" 출력
			System.out.println("0보다 크지 않음");
		}
		

	} // end main()

} // end IfMain02
package edu.java.if03;

import java.util.Scanner;

public class IfMain03 {

	public static void main(String[] args) {
		System.out.println("if - else if - else문");
		/*
		 * if(조건1) { 본문A } else if(조건2) { 본문B } ... else { 본문C } - 조건1이 참일 경우 본문A, 조건2가
		 * 참일 경우 본문B, 둘 다 아닐 경우 본문 C 실행 - else if 절은 1개 이상 작성 가능
		 */

		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();

		if (x > 0) {
			System.out.println("양수");
		} else if (x == 0) {
			System.out.println("0");
		} else {
			System.out.println("음수");
		}

		sc.close();

	} // end main()

} // end IfMain03
package edu.java.if04;

import java.util.Scanner;

public class IfMain04 {

	public static void main(String[] args) {
		System.out.println("if - else if - else 연습");
		
		// 점수를 정수로 입력받아서 score 이름의 변수에 저장
		// score가 90점 이상이면 "A"를 출력
		// score가 80점 이상이면 "B"를 출력
		// score가 70점 이상이면 "C"를 출력
		// score가 70점 미만이면 "F"를 출력
		
		Scanner sc = new Scanner(System.in);
		
		int score = sc.nextInt();
		System.out.println("점수 등급은? ");
		
		if(score >= 90) {
			System.out.println("A");
		} else if(score >= 80) {
			System.out.println("B");
		} else if(score >= 70) {
			System.out.println("C");
		} else {
			System.out.println("F");
		}

	} // end main()

} // end IfMain04
package edu.java.if05;

import java.util.Scanner;

public class IfMain05 {

	public static void main(String[] args) {
		System.out.println("if-else if 문자 조건 연습");
		
		// 문자 하나를 입력받아서 그 글자가
		// 1) A ~ Z이면, "영대문자"라고 출력
		// 2) a ~ z이면, "영소문자"라고 출력
		// 3) 그 외 경우는, "몰라요"라고 출력
		
		Scanner sc = new Scanner(System.in);
		System.out.println("문자를 입력하세요 : ");
		String str = sc.next();
		char ch = str.charAt(0);
		
		if('A' <= ch && ch <= 'Z') {
			System.out.println("영대문자");
		} else if('a' <= ch && ch <= 'z') {
			System.out.println("영소문자");
		} else {
			System.out.println("몰라요");
		}
		
	} // end main()

} // end IfMain05

0개의 댓글