Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

Test01 : if_switch 본문

JAVA/chapter04_control

Test01 : if_switch

GAWON 2023. 5. 25. 16:50
package org.joonzis.test;

import java.util.Scanner;

public class Test01 {
	public static void main(String[] args) {
//		Test01.java
//		Q. 정수를 입력 받아 "짝수", "홀수" 구분해서 출력하기
//		   3의 배수는 "3의 배수"로 출력하기
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수 입력 >> ");
		int num = sc.nextInt();
		
		// 1. 방법 1 - 그룹
		if(num % 3 == 0) {
			System.out.println("3의 배수");
		}else if(num % 2 == 0) {
			System.out.println("짝수");
		}else {
			System.out.println("홀수");
		}
		
		// 2. 방법 2 - 짝 or 홀 + 3의 배수
		if(num % 2 == 0) {
			System.out.println("짝수");
		}else {
			System.out.println("홀수");
		}
		if(num % 3 == 0) {
			System.out.println("3의 배수");
		}
		
		
		
		sc.close();
		
		
	} 
}
package org.joonzis.test;

import java.util.Scanner;

public class Test02 {
	public static void main(String[] args) {
//		Test02.java
//		Q. 필기와 실기 점수를 입력 받아 합격 유무 출력하기
//			합격기준: 필기와 실기 점수 모두 70점 이상이거나, 평균이 80 이상시 "합격" 아니면 "불합격"
		
		Scanner sc = new Scanner(System.in);
		System.out.print("필기 점수 >> ");
		int num1 = sc.nextInt();
		System.out.print("실기 점수 >> ");
		int num2 = sc.nextInt();
		
		double avg = (double)(num1+num2) / 2.0;
		
		
		// 1. 논리식 사용 x
		if(avg >= 80) {
			System.out.println("합격");
		}else if(num1 >= 70) {	// 필기 70 이상
			if(num2 >= 70) {	// 실기 70 이상
				System.out.println("합격");
			}
		}else {
			System.out.println("불합격");
		}
		
		// 2. 논리식 사용
		if(  (num1 >=70 && num2 >=70) || avg >=80 ) {
			System.out.println("합격");
		}else {
			System.out.println("불합격");
		}
		
		
		
		sc.close();
	}
}
package org.joonzis.test;

import java.util.Scanner;

public class Test03 {
	public static void main(String[] args) {
//		Test03.java
//		Q. 나이를 입력 받아 7 이하면 "미취학", 13 이하이면 "초등학생", 
//		16 이하이면 "중학생", 19 이하이면 "고등학생", 나머지는 "성인"
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("나이 입력 >> ");
		int age = sc.nextInt();
		
		
		if(age <= 7) {
			System.out.println("미취학");
		}else if(age <= 13) {
			System.out.println("초등학생");
		}else if(age <= 16) {
			System.out.println("중학생");
		}else if(age <= 19) {
			System.out.println("고등학생");
		}else {
			System.out.println("성인");
		}
		
		
		
		sc.close();
		
		
	}
}
package org.joonzis.test;

import java.util.Scanner;

public class Test05 {
	public static void main(String[] args) {
//		Test05.java
//		Q. 문자를 하나 입력 받아 "대문자", "소문자", "아라비아 숫자", "일반 문자" 구분해서 출력하기
//		     아스키 코드표 참고
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("문자 입력 >> ");
		char ch = sc.next().charAt(0);
		
		if(ch >= 'A' && ch <= 'Z') {
			System.out.println("대문자");
		}else if(ch >= 'a' && ch <= 'z') {
			System.out.println("소문자");
		}else if(ch >= '0' && ch <= '9') {
			System.out.println("아라비아 숫자");
		}else {
			System.out.println("일반 문자");
		}
		
		
		
		sc.close();
		
		
	}
}
package org.joonzis.test;

import java.util.Scanner;

public class Test06 {
	public static void main(String[] args) {
//		Test06.java
//		Q. 알파벳을 하나 입력 받아 대문자가 입력되면 소문자로 변환해서 출력하고, 소문자가 입력되면 대문자로 변환해서 출력하고
//		   	나머지 문자들은 그대로 출력하기
//		★아스키 코드  
//		97~122 : 소문자,
//		 65~90 : 대문자, 
//		 48~57 : 숫자
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("문자 입력 >> ");
		char ch = sc.next().charAt(0);
		
		if(ch >= 'A' && ch <= 'Z') {
			System.out.println(ch+=32);
		}else if(ch >= 'a' && ch <= 'z') {
			System.out.println(ch-=32);
		}else {
			System.out.println(ch);
		}
		
		sc.close();
	}
}
package org.joonzis.test;

import java.util.Scanner;

public class Test08 {
	public static void main(String[] args) {
//		Test08.java (switch문)
//		Q. 권한을 출력하기
//			3 수준 : 실행, 쓰기, 읽기 / 2수준 : 쓰기, 읽기 / 1수준 : 읽기
//			실행 예)
//			수준입력 >> 3
//			출력 : 실행, 쓰기, 읽기
		
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("수준 입력 >>");
		int num = sc.nextInt();
		
//		switch (num) {
//		case 1: System.out.println("읽기");
//			break;
//		case 2: System.out.println("쓰기, 읽기");
//			break;
//		case 3: System.out.println("실행, 쓰기, 읽기");
//			break;
//		}
		
		switch (num) {
		case 3: System.out.print("실행, ");
		case 2: System.out.print("쓰기, ");
		case 1: System.out.println("읽기");
			break;
		}
		
		
		
		sc.close();
	}
}
package org.joonzis.test;

import java.util.Scanner;

public class Test09 {
	public static void main(String[] args) {
//		Test09.java (switch문)
//		Q. 월을 입력 받아 일을 출력하기
//			실행 예)
//			월 입력 >> 10
//			출력 : 10월은 31일 까지 있습니다.
		Scanner sc = new Scanner(System.in);
		
		System.out.print("수준 입력 >>");
		int mon = sc.nextInt();
		
		
		switch (mon) {
		case 2: System.out.println(mon + "월은 28일 까지 있습니다.");
			break;
		case 4:
		case 6:
		case 9:
		case 11:System.out.println(mon + "월은 30일 까지 있습니다.");
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:System.out.println(mon + "월은 31일 까지 있습니다.");
			break;
		}
		
		
		
	}
}

'JAVA > chapter04_control' 카테고리의 다른 글

Test02 : for_while  (0) 2023.05.25
chapter07 : continue  (0) 2023.05.25
chapter06 : break  (0) 2023.05.25
chapter05 : for  (0) 2023.05.25
chapter04 : do_while  (0) 2023.05.25