관리 메뉴

거니의 velog

230707 자바 강의 본문

대덕인재개발원_Java

230707 자바 강의

Unlimited00 2023. 7. 12. 19:43

[TrinominalOperatorExample.java]

package ddit.chap03.sec01;

import java.util.Scanner;

public class TrinominalOperatorExample {

	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		
		methods1();
		methods2();
		
	}
	
	public static void methods1() {
		
		// 삼항연산자
		// (조건문) ? 명령문1 : 명령문 2
		// - 조건이 참(true) ? 명령문 1을 수행하고, 조건이 거짓(false)이면 ? 명령문 2를 수행
		// 여러개의 if문 대신에 사용하여 여러 명령문을 축약해서 작성이 가능하다.
				
		// 나이를 입력 받아 성년인지 미성년인지를 판단
		
		System.out.print("나이? : ");
		int age = Integer.parseInt(sc.nextLine());
				
		String str = (age >= 18) ? "성년자 입니다." : "미성년자 입니다.";
				
		System.out.println(str);
		
	}
	
	public static void methods2() {
		
		// 키보드로 입력받은 점수가
		// 90-100 : A학점
		//  80-89 : B학점
		//  70-79 : C학점
		//  60-69 : D학점
		//  그 이하 : F학점을 출력
		
		System.out.print("점수 : ");
		int score = sc.nextInt();
//		String str = (score >= 90 ? "A학점" : (score >= 80 ? "B학점" : 
//					 (score >= 70 ? "C학점" : (score >= 60 ? "D학점" : "F학점"))));
		String str = (score >= 90) ? "A학점" : (score >= 80) ? "B학점" : 
					 (score >= 70) ? "C학점" : (score >= 60) ? "D학점" : "F학점";
		System.out.println(score + "점 => " + str);
		
	}

}

[IfStatementExample.java]

package ddit.chap04.sec01;

import java.util.Scanner;

public class IfStatementExample {

	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		
//		methods01();
		methods02();
		
	}

	public static void methods01() {
		
		// 점수를 하나 입력 받아 60점 이상이면 "합격"을 출력하시오.
		System.out.print("점수 : ");
		int score = sc.nextInt();
		if(score >= 60) System.out.println("합격");
		
	}
	
	public static void methods02() {
		
		// 점수를 하나 입력 받아 60점 이상이면 "합격", 60점 미만이면 "불합격"을 출력하시오.
		System.out.print("점수 : ");
		int score = sc.nextInt();
		if(score >= 60) {
			System.out.println("합격");
		}else {
			System.out.println("불합격");
		}
		
	}
	
}

class Account {
	
	private static Account instance = null;
	// 생성자 메서드는 첫글자가 대문자여야 한다. 클래스명과 같아야 하므로.
	// 객체가 하나만 생성되는 싱글턴 패턴이 됨.
	private Account() {}
	public static Account getInstance() {
		if(instance==null) instance = new Account();
		return instance;
	}
	
}

[IfHomework_01.java]

package ddit.chap04.sec01;

import java.text.DecimalFormat;
import java.util.Scanner;

public class IfHomework_01 {

	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {

//		method1();
//		method2();
		method3();

	}

	public static void method1() {

		// if문 예제 ) 1 ~ 12월에 해당하는 월 하나를 입력받아
		// 3 ~ 5월 : "봄"
		// 6 ~ 8월 : "여름"
		// 9 ~ 11월 : "가을"
		// 12, 1, 2월 : "겨울"을 출력

		System.out.print("몇 월? : ");
		int month = Integer.parseInt(sc.nextLine());

//		String season = (month >= 3 && month <= 5) ? "봄" : 
//						(month >= 6 && month <= 8) ? "여름" : 
//						(month >= 9 && month <= 11) ? "가을" :
//						(month == 12 || (month >=1 && month <=2)) ? "겨울" : 
//						"해당하는 월이 없습니다.";

		String season = "";

		if (month < 0 || month > 12) {

			System.out.println("잘못된 월 입력입니다.");

		} else {

			if (month >= 3 && month <= 5) {
				season = "봄";
			} else if (month >= 6 && month <= 8) {
				season = "여름";
			} else if (month >= 9 && month <= 11) {
				season = "가을";
			} else {
				season = "겨울";
			}

			System.out.println(season);

		}

	}

	public static void method2() {

		// if문 예제2 ) 점수를 입력받아 그 값이
		// 100-97 : A+
		// 96-93 : A0
		// 92-90 : A-
		// 89-87 : B+
		// 86-83 : B0
		// 82-80 : B-
		// 그 이하는 F를 출력하시오.

		System.out.print("몇 점? : ");
		int score = Integer.parseInt(sc.nextLine());
		String grade = "";

		if (score < 0 || score > 100) {

			System.out.println("잘못된 점수 입력입니다.");

		} else {
			if (score >= 90) {
				grade = "A";
				if (score > 96) {
					grade += "+"; // grade = grade + "+"
				} else if (score > 92) {
					grade += "0"; // grade = grade + "0"
				} else {
					grade += "-"; // grade = grade + "-"
				}
			} else if (score >= 80) {
				grade = "B";
				if (score > 86) {
					grade += "+"; // grade = grade + "+"
				} else if (score > 82) {
					grade += "0"; // grade = grade + "0"
				} else {
					grade += "-"; // grade = grade + "-"
				}
			} else {
				grade = "F";
			}
			
			System.out.println(score + " 점의 학점은 " + grade + " 입니다.");
			
		}

	}
	
	public static void method3() {
		
		// if문 예제3 ) 키(m)와 체중(kg)을 입력하여 BMI 지수를 구하고 BMI 지수에 따른 판정을 하시오.
		
		//            BMI지수 = 체중  / (키 * 키);
		//
		//            0 ~ 18.4    : 저체중
		//            18.5 ~ 22.9 : 정상
		//            23.0 ~ 24.9 : 과체중
		//            25.0 ~ 29.9 : 비만
		//            30.0 이상         : 고도비만
		
		System.out.print("키(m)를 입력하시오 : ");
		double height = sc.nextDouble();
		
		System.out.print("체중(kg)를 입력하시오 : ");
		double weight = sc.nextDouble();
		
		DecimalFormat format = new DecimalFormat("##.##");
		double bmi = (weight / (height * height)) * 10000;
		bmi = Double.parseDouble(format.format(bmi));
		System.out.println(bmi);
		
		if(bmi < 0) {
			System.out.println("올바른 bmi 수가 아닙니다.");
		}else {
			if(bmi <= 18.4) {
				System.out.println("저체중");
			}else if(bmi <= 22.9) {
				System.out.println("정상");
			}else if(bmi <= 24.9) {
				System.out.println("과체중");
			}else if(bmi <= 29.9) {
				System.out.println("비만");
			}else {
				System.out.println("고도비만");
			}
		}
		
		// 0.0 <= Math.random() < 1.0
		// 0.0 <= Math.random()*10 < 10.0
		// 0 <= (int) Math.random*10 < 10
		// 1 <= (int) Math.random*10+1 < 11
		
		// (int) (Math.random()*구간상한값)+구간하한값 -> 난수발생식.
		
	}

}

[RockPaperScissorsHomework.java]

package ddit.chap04.sec01;

import java.util.Scanner;

public class RockPaperScissorsHomework {

	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		
		// 컴퓨터와 사용자가 가위, 바위, 보 게임을 하려고 한다. 이를 프로그래밍 하시오.
		// 가위 : 1, 바위 : 2, 보 : 3
		
		// 0.0 <= Math.random() < 1.0
		// 0.0 <= Math.random()*10 < 10.0
		// 0 <= (int) Math.random*10 < 10
		// 1 <= (int) Math.random*10+1 < 11
				
		// (int) (Math.random()*구간상한값)+구간하한값 -> 난수발생식.
		
		int computer = (int) (Math.random() * 3) + 1;
		
//		System.out.print("가위 : 1, 바위 : 2, 보 : 3 => ");
//		int user = sc.nextInt();
		
		System.out.print("가위 ? 바위 ? 보? => ");
		String str = sc.nextLine();
		int user = 0;
		if(str.equals("가위")) {
			user = 1;
		}else if(str.equals("바위")) {
			user = 2;
		}else if(str.equals("보")) {
			user = 3;
		}else {
			user = -1;
		}
		
		System.out.println("유저 : " + user);
		System.out.println("컴퓨터 : " + computer);
		
		// 가위 : 1, 바위 : 2, 보 : 3
		// 유저가 가위를 낸 경우 : 1
			// 컴퓨터가 가위를 냈다면 : 1 = 무승부.
			// 컴퓨터가 바위를 냈다면 : 2 = 컴퓨터가 이겼습니다.
			// 컴퓨터가 보를 냈다면    : 3 = 유저가 이겼습니다.
		// 유저가 바위를 낸 경우 : 2
			// 컴퓨터가 가위를 냈다면 : 1 = 유저가 이겼습니다.
			// 컴퓨터가 바위를 냈다면 : 2 = 무승부.
			// 컴퓨터가 보를 냈다면    : 3 = 컴퓨터가 이겼습니다.
		// 유저가 보를 낸 경우 : 3
			// 컴퓨터가 가위를 냈다면 : 1 = 컴퓨터가 이겼습니다.
			// 컴퓨터가 바위를 냈다면 : 2 = 유저가 이겼습니다.
			// 컴퓨터가 보를 냈다면    : 3 = 무승부.
		
//		if(user <= 0 || user > 3) {
//		System.out.println("1 ~ 3사이의 정수만 입력하세요.");		
		
		if(user == -1) {
			System.out.println("가위, 바위, 보만 입력하세요.");
		}else {
			if(user == 1) {
				if(computer == 1) System.out.println("유저 : 가위 , 컴퓨터 : 가위 / 무승부");
				if(computer == 2) System.out.println("유저 : 가위 , 컴퓨터 : 바위 / 컴퓨터가 이겼습니다.");
				if(computer == 3) System.out.println("유저 : 가위 , 컴퓨터 : 보 / 유저가 이겼습니다.");
			}else if(user == 2) {
				if(computer == 1) System.out.println("유저 : 바위 , 컴퓨터 : 가위 / 유저가 이겼습니다.");
				if(computer == 2) System.out.println("유저 : 바위 , 컴퓨터 : 바위 / 무승부");
				if(computer == 3) System.out.println("유저 : 바위 , 컴퓨터 : 보 / 컴퓨터가 이겼습니다.");
			}else if(user == 3) {
				if(computer == 1) System.out.println("유저 : 보 , 컴퓨터 : 가위 / 컴퓨터가 이겼습니다.");
				if(computer == 2) System.out.println("유저 : 보 , 컴퓨터 : 바위 / 유저가 이겼습니다.");
				if(computer == 3) System.out.println("유저 : 보 , 컴퓨터 : 보 / 무승부.");
			}
		}
		
	}

}

'대덕인재개발원_Java' 카테고리의 다른 글

230711 자바 강의  (0) 2023.07.12
230710 자바 강의  (0) 2023.07.12
230706 자바 강의  (0) 2023.07.12
230705 자바 강의  (0) 2023.07.12
230704 자바 강의  (0) 2023.07.11