문제는 저작권 문제로 따로 올리지 않는다.
실습 코드는 모두 해당 클래스 내부에 작성되어 있다.
package com.kh.practice.chap01;
import java.util.Scanner;
public class ControlPractice {
}
public void practice6() {
Scanner sc = new Scanner(System.in);
System.out.print("권한을 확인하고자 하는 회원 등급 : ");
String member = sc.nextLine();
String right = "";
switch(member) {
case "관리자":
right = "회원관리, 게시글 관리, 게시글 작성, 게시글 조회, 댓글 작성";
break;
case "회원":
right = "게시글 작성, 게시글 조회, 댓글 작성";
break;
case "비회원":
right = "게시글 조회";
break;
default:
System.out.println("다시 입력해주세요.");
return;
}
System.out.print(right);
} // switch문으로 ,까지 제어할순 없을까
public void practice7() {
Scanner sc = new Scanner(System.in);
System.out.print("키(m)를 입력해 주세요 : ");
double height = sc.nextDouble();
System.out.print("몸무게(kg)를 입력해 주세요 : ");
double weight = sc.nextDouble();
double BMI = weight / (height * height);
String result = "";
if (BMI < 18.5) {
result = "저체중";
} else if (BMI >= 18.5 && BMI < 23) {
result = "정상체중";
} else if (BMI >= 23 && BMI < 25) {
result = "과체중";
} else if (BMI >= 25 && BMI < 30) {
result = "비만";
} else {
result = "고도 비만";
}
System.out.printf("BMI 지수 : %.14f \\n", BMI);
System.out.println(result);
}
<aside> 📢 코멘트 7번문제 피드백드리면 조건식에 bmi >= 18.5 && bmi < 23 이부분 같은경우 앞의 bmi >= 18.5조건식은 필요가 없는 조건식이므로 없애주시는게 가독성도 좋고, 효율도 좋을것!
</aside>
public void practice8() {
Scanner sc = new Scanner(System.in);
System.out.print("피연산자1 입력 : ");
int num1 = sc.nextInt();
System.out.print("피연산자2 입력 : ");
int num2 = sc.nextInt();
sc.nextLine(); // 위의 예제에서 사용해서 버퍼를 비우는 용도
System.out.print("연산자를 입력(+,-,/,%) : ");
String oper = sc.nextLine();
double result = 0;
if (num1 > 0 && num2 > 0) {
switch(oper) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "/":
result = (double) num1 / num2;
break;
case "%":
result = (double) num1 % num2;
break;
default:
System.out.println("잘못 입력하셨습니다. 프로그램을 종료합니다.");
return;
}
} else {
System.out.println();
practice8();
return;
}
System.out.printf("%d %s %d = %.6f", num1, oper, num2, result);
}
public void practice9() {
Scanner sc = new Scanner(System.in);
System.out.print("중간 고사 점수 : ");
int midEx = sc.nextInt();
System.out.print("기말 고사 점수 : ");
int finEx = sc.nextInt();
System.out.print("과제 점수 : ");
int hw = sc.nextInt();
System.out.print("출석 회수 : ");
int att = sc.nextInt();
double midExScore = midEx * 0.2;
double finExScore = finEx * 0.3;
double hwScore = hw * 0.3;
double attScore = att;
double total = midExScore + finExScore + hwScore + attScore;
System.out.println("================= 결과 =================");
// 수정 코드
if (att <= 20-(20*0.3)) {
System.out.printf("Fail [출석 회수 부족 (%d/20)] \\n", att);
} else {
System.out.printf("중간 고사 점수(20) : %.1f \\n", midExScore);
System.out.printf("기말 고사 점수(30) : %.1f \\n", finExScore);
System.out.printf("과제 점수 (30) : %.1f \\n", hwScore);
System.out.printf("출석 점수 (20) : %.1f \\n", attScore);
System.out.printf("총점 : %.1f \\n", total);
if (total >= 70) {
System.out.println("PASS");
} else {
System.out.println("Faile [점수 미달]");
}
}
}