본문 바로가기
Java

[Java 자바] 연산자

by sycareer 2021. 4. 25.

1. 양수/음수 구별하기

2. 짝수/홀수 구별하기

3. 몫/나머지 출력하기

4. 입력받은 데이터로 조건에 맞는 결과 출력하기

5. 조건식 사용하기 

   (조건식1) ? 1이 참일 경우 결과 : (조건식2) ? 2가 참일 경우 결과 : 거짓일 경우 결과;

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.sy.practice1.example;
 
import java.util.Scanner;
 
public class C_OperatorPractice {
 
    //연산자 실습
    
    //연산자1. 입력받은 정수가 양수인지 아닌지 구별하여 출력
    public void practice1() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("정수 : ");
        int num = sc.nextInt();
        
        String result = (num > 0) ? "양수다." : "양수가 아니다.";        // (조건식) ? 참일 경우 결과 : 거짓일 경우 결과;
        
        System.out.println(result);
    }
    
    
    //연산자2. 입력받은 정수가 양수, 0, 음수인지 구별하여 출력
    public void practice2() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("정수 : ");
        int num = sc.nextInt();
        
        String result = (num > 0) ? "양수다." : (num == 0) ? "0이다." : "음수다.";    // (조건식1) ? 1이 참일 경우 결과 : (조건식2) ? 2가 참일 경우 결과 : 거짓일 경우 결과;
        
        System.out.println(result);
    }
    
    
    //연산자3. 입력받은 정수가 짝수인지 홀수인지 구별하여 출력
    public void practice3() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("정수 : ");
        int num = sc.nextInt();
        
        String result = (num % 2 == 0) ? "짝수다." : "홀수다.";
        
        System.out.println(result);
    }
    
    
    //연산자4. 인원수, 사탕 개수를 입력받고 1인당 똑같이 나눠가진 사탕 개수와 남는 사탕의 개수 출력
    public void practice4() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("인원수 : ");
        int people = sc.nextInt();
        
        System.out.print("사탕 개수 : ");
        int candy = sc.nextInt();
        
        System.out.println("1인당 사탕 개수 : " + (candy / people));
        System.out.println("남는 사탕 개수 : " + (candy % people));
    }
    
    
    //연산자5. 입력받은 값을 변수에 저장하고 출력하기. 성별이 'M'이면 남학생. 아니면 여학생으로 출력.
    public void practice5() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("이름 : ");
        String name = sc.nextLine();
        
        System.out.print("학년(숫자만) : ");
        int year = sc.nextInt();
        
        System.out.print("반(숫자만) : ");
        int cla = sc.nextInt();
        
        System.out.print("번호(숫자만) : ");
        int num = sc.nextInt();
        sc.nextLine();                        //nextInt() 다음에 nextLine()이 오기 때문에 버퍼 비우기.
        
        System.out.print("성별(M/F) : ");
        char gen = sc.nextLine().charAt(0);
        
        System.out.print("성적(소수점 아래 둘째 자리까지) : ");
        double grade = sc.nextDouble();
        
        String afterGen = (gen == 'M' || gen == 'm') ? "남학생" : "여학생";
        
        System.out.println(year + "학년 " + cla + "반 " + num + "번 " + name + " " + afterGen + "의 성적은 " + grade + "이다.");
    }
    
    
    //연산자6. 나이를 입력받아 어린이(13세 이하), 청소년(13세 초과 19세 이하), 성인(19세 초과) 구분하여 출력
    public void practice6() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("나이 : ");
        int age = sc.nextInt();
        
        String result = (age > 19) ? "성인" : (age > 13) ? "청소년" : "어린이";
        
        System.out.println(result);
    }
    
    
    //연산자7. 과목별 점수 정수로 입력받아 합계(정수형)와 평균(실수형) 구하고, 합격 여부 출력하기(각 과목 40점 이상이고, 전과목 평균 60점 이상인 경우 합격)
    public void practice7() {
        
        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;        //int형인 sum을 3.0인 double형으로 나눔으로서 자동 형변환.
        String result = ((kor >= 40&& (eng >= 40&& (math >= 40&& (avg >= 60)) ? "합격" : "불합격";
        
        System.out.println("합계 : " + sum);
        System.out.println("평균 : " + avg);
        System.out.println(result);    
    }
    
    
    //연산자8. 입력받은 주민번호를 통해 성별 출력하기
    public void practice8() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("주민번호를 입력하세요(- 포함) : ");
        String rrn = sc.nextLine();                //rrn : Resident Registration Number
        
        char num = rrn.charAt(7);
        String gen = (num == '2' || num == '4') ? "여자" : (num == '1' || num == '3') ? "남자" : "유효하지 않은 주민번호입니다.";        
        //rrn 문자열의 8번째 문자위치(인덱스 7)가 2 또는 4이면 여자. char은 문자형이기 때문에 숫자에 작은따옴표를 붙여 문자형으로 형변환 해줌.
        
        System.out.println(gen);
    }
    
    
    //연산자9. 정수를 입력받고, 세 번째 입력한 정수가 첫 번째 입력한 정수 이하거나 두 번째 입력한 정수 초과이면 true 출력. (단, 첫 번째 정수는 두 번째 정수보다 작아야 함)
    public void practice9() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("정수1 : ");
        int num1 = sc.nextInt();
        
        System.out.print("정수2(정수1보다 작은 값) : ");
        int num2 = sc.nextInt();
        
        System.out.print("정수3 : ");
        int num3 = sc.nextInt();
        
        boolean result = (num3 <= num1) || (num3 > num2);
        
        System.out.println(result);
    }
    
    
    //연산자10. 입력받은 세 개의 정수가 모두 같으면 true 출력
    public void practice10() {
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("정수1 : ");
        int num1 = sc.nextInt();
        
        System.out.print("정수2 : ");
        int num2 = sc.nextInt();
        
        System.out.print("정수3 : ");
        int num3 = sc.nextInt();
        
        boolean result = (num1 == num2) && (num2 == num3);
        
        System.out.println(result);
    }
    
    
    //연산자11. A, B, C사원의 연봉을 입력받고 인센티브를 포함한 연봉 출력. 인센포함 급여가 3000만 원 이상이면 "3000 이상", 그 외는 "3000 미만" 출력.
    //          인센티브 비율 --> A : 0.4, B : 없음, C : 0.15
    public void practice11() {
        //데이터 입력
        Scanner sc = new Scanner(System.in);
        
        System.out.print("A사원의 연봉(만 원 단위) : ");
        int salaryA = sc.nextInt();
        
        System.out.print("B사원의 연봉(만 원 단위) : ");
        int salaryB = sc.nextInt();
        
        System.out.print("C사원의 연봉(만 원 단위) : ");
        int salaryC = sc.nextInt();
        
        //인센티브 비율 : 나중에 인센티브 비율이 변동되면 수정하기 쉽도록 따로 변수로 선언 해줌.
        double incentA = 0.4;
        double incentB = 0;
        double incentC = 0.15;
        
        //인센티브 포함 연봉
        double incSalA = salaryA * (1 + incentA);
        double incSalB = salaryB * (1 + incentB);
        double incSalC = salaryC * (1 + incentC);
        
        //3000만 원 이상 확인
        String resultA = (incSalA >= 3000) ? "3000 이상" : "3000 미만";
        String resultB = (incSalB >= 3000) ? "3000 이상" : "3000 미만";
        String resultC = (incSalC >= 3000) ? "3000 이상" : "3000 미만";
        
        //결과 출력
        System.out.println("A사원의 인센티브포함 연봉 : " + incSalA + "\n" + resultA);
        System.out.println("B사원의 인센티브포함 연봉 : " + incSalB + "\n" + resultB);
        System.out.println("C사원의 인센티브포함 연봉 : " + incSalC + "\n" + resultC);
    }
 
}
 
cs