모든 사람이 사탕을 골고루 나눠가지려고 한다. 인원 수와 사탕 개수를 키보드로 입력 받고, 1인당 동일하게 나눠가진 사탕 개수와 나눠주고 남은 사탕의 개수를 출력하세요.
(1) 내 풀이
Scanner sc = new Scanner(System.in);
System.out.print("인원 수 : ");
int num1 = sc.nextInt();
System.out.print("사탕 개수 : ");
int num2 = sc.nextInt();
int result1 = num2 / num1;
int result2 = num2 - result1*num1;
System.out.println("");
System.out.println("1인당 사탕 개수 : " + result1);
System.out.println("남는 사탕 개수 : " + result2);
(2) 다른 풀이
Scanner sc = new Scanner(System.in);
System.out.print("인원 수 : ");
int input = sc.nextInt();
System.out.print("사탕 개수 : ");
int candy = sc.nextInt();
System.out.println("1인당 사탕 개수 : " + candy / input );
System.out.println("남는 사탕 개수 : " + candy % input );
// 굳이 result 1, 2로 새로운 변수를 만들기보다는
// println()에 바로 수식을 넣어 계산
키보드로 입력 받은 값들을 변수에 기록하고 저장된 변수 값을 화면에 출력하여 확인하세요.
Scanner sc = new Scanner(System.in);
System.out.print("이름 : ");
String name = sc.next();
System.out.print("학년(정수) : ");
int grade = sc.nextInt();
System.out.print("반(정수) : ");
int class1 = sc.nextInt();
System.out.print("번호(정수) : ");
int number = sc.nextInt();
System.out.print("성별(남학생/여학생) : ");
String gender = sc.next();
System.out.print("성적(소수점 아래 둘째 자리까지) : ");
double test = sc.nextDouble();
System.out.println("");
System.out.printf("%d학년 %d반 %d번 %s %s의 성적은 %.2f점 입니다.", grade, class1, number, gender, name, test);
정수를 하나 입력 받아 짝수/홀수를 구분하세요. (0은 짝수로 취급합니다.)
(1) 내 풀이
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int input = sc.nextInt();
String result;
if(input %2 == 0) {
result = "짝수";
} else {
result = "홀수";
}
System.out.println(result + " 입니다.");
(2) 모범 풀이
Scanner sc = new Scanner(System.in);
System.out.print("정수 입력 : ");
int input = sc.nextInt();
String result = input % 2 == 0 ? "짝수" : "홀수";
// 삼항 연산자를 사용하여 수식을 한 줄로 줄이면 훨씬 간단히 표기할 수 있음!
System.out.println(result + " 입니다.");
국어, 영어, 수학에 대한 점수를 키보드를 이용해 정수로 입력 받고,
세 과목에 대한 합계(국어+영어+수학)와 평균(합계/3.0)을 구하세요.
세 과목의 점수와 평균을 가지고 합격 여부를 처리하는데
세 과목 점수가 각각 40점 이상이면서 평균이 60점 이상일 때 합격, 아니라면 불합격을 출력하세요.
(1) 내 풀이 -> if - else if - else 문 활용
Scanner sc = new Scanner(System.in);
System.out.print("국어 : ");
int korean = sc.nextInt();
System.out.print("영어 : ");
int english = sc.nextInt();
System.out.print("수학 : ");
int math = sc.nextInt();
int sum = korean + english + math;
double average = (korean + english + math)/3.0;
// (korean + english + math) -> sum으로 바꿔서 표기
String result;
if(korean < 40 || english < 40 || math < 40) {
result = "불합격";
} else if(korean >= 40 && english >= 40 && math >= 40 && average >= 60.0) {
result = "합격";
} else {
result = "불합격";
}
System.out.println("");
System.out.println("합계 : " + sum);
System.out.println("평균 : " + average);
System.out.println(result);
(2) 다른 풀이 -> 삼항 연산자 활용
Scanner sc = new Scanner(System.in);
System.out.print("국어 : ");
int kor = sc.nextInt();
System.out.print("영어 : ");
int eng = sc.nextInt();
System.out.print("수학 : ");
int math = sc.nextInt();
int sum = kor + eng + math; // 합계
double avg = sum / 3.0; // 평균
System.out.println("합계 : " + sum);
System.out.printf("평균 : %.1f\n", avg);
boolean result = (kor >= 40) && (eng >= 40) && (math >= 40) && (avg >= 60);
System.out.println( result ? "합격" : "불합격" );
// 조건식 ? 식1 : 식2
조건식을 작성할 때 삼항 연산자를 활용하여 코드를 간단하게 줄이는 연습을 하자.
그리고 굳이 필요 없는 변수는 만들지 않아도 될 것 같다....
변수를 만들기 전에 이미 있는 변수를 활용하여 코드를 작성하는 방법을 고안해 보자!