-
※문제. 국어, 영어, 수학 점수를 입력받고, 과목의 점수와 평균을 가지고 합격 여부 출력
합격의 조건 : 세 과목의 점수가 각각 40점이상이면서, 평균이 60점 이상. 더보기1234567891011121314151617181920public void opSample() {Scanner sc = new Scanner(System.in);System.out.println("국어 점수를 입력하시오 > ");int kor = sc.nextInt();System.out.println("영어 점수를 입력하시오 > ");int eng = sc.nextInt();System.out.println("수학 점수를 입력하시오 > ");int math = sc.nextInt();int sum = kor + eng + math;double average = sum/3.0;boolean subBool = kor >= 40 && eng >= 40 && math >= 40;boolean aveBool = average >= 60;String result = subBool && aveBool? "합격" : "불합격";System.out.println(result);}cs result에 바로 boolean 값을 넣으려니 너무 길어져서 따로 빼줬다.
이게 확실히 보기 더 나은 듯.
※문제. 학생 이름, 학년, 반, 번호, 성별(M/F), 성적(소수점 2자리)을 입력받은 후 출력3학년 2반 24번 남학생 홍길동은 성적이 95.55이다. 더보기1234567891011121314151617181920public void opSample2() {Scanner sc = new Scanner(System.in);System.out.println("이름을 입력하시오 > ");String studentName = sc.next();System.out.println("학년을 입력하시오 > ");int studentGrade = sc.nextInt();System.out.println("반을 입력하시오 > ");int studentClass = sc.nextInt();System.out.println("번호를 입력하시오 > ");int studentNum = sc.nextInt();System.out.println("성별을 입력하시오(M/F) > ");char gender = sc.next().charAt(0);String studentGender = (gender == 'M')? "남학생" : "여학생";System.out.println("성적을 입력하시오 > ");double studentScore = sc.nextDouble();System.out.printf("%d학년 %d반 %d번 %s %s은 성적이 %.2f이다.%n",studentGrade, studentClass, studentNum, studentGender, studentName, studentScore);}cs 변수명이...길다 ㅋㅋㅋㅋ
보고 바로 이해 가는 변수명과 깔끔한 변수명 사이 어딘가를 항상 고민하고 있다.
stuG, stuClass 정도면 그래도 보면 바로 알아차릴 수 있지 않을까 싶기도 하다.
'혼자 있는 방 > Java' 카테고리의 다른 글
Homework(4) (0) 2021.06.22 Homework(3) (0) 2021.06.21 Homework(1) (0) 2021.06.17 자바 라이브러리 (0) 2021.06.07 인터페이스(interface) (0) 2021.06.03 댓글