728x90
반응형
package practice.operator;
public class OperatorPractice {
public void method1() {
//문제1 출력 값 예측
int num1 = 10, num2 = 0;
String str = "hello";
num1 += 20; //num1 : 30
num2 += 10; //num2 : 10
num1 /= num2; //num1 : 3
num2 *= num1; //num2 : 30
str += num2; //str : hello30
num2 -= num1; //num2 : 27
}
}
package practice.operator;
import java.util.Scanner;
public class OperatorPractice {
public void method2() {
//문제2 삼항연산자 두개의 정수의 짝홀 판별
Scanner sc = new Scanner(System.in);
System.out.print("첫 번째 정수 입력 : ");
int num1 = sc.nextInt();
System.out.print("두 번째 정수 입력 : ");
int num2 = sc.nextInt();
String str1 = "하나는 홀수이고 하나는 짝수이다";
String str2 = "두 수 모두 홀수이다";
String str3 = "두 수 모두 짝수이다";
String result = (((num1%2) != (num2%2)) ? str1 : ((num1%2 == 0) ? str3 : str2));
System.out.println(result);
}
}
package practice.operator;
public class Run {
public static void main(String[] args) {
OperatorPractice op = new OperatorPractice();
op.method2();
}
}
//첫 번째 정수 입력 : 7
//두 번째 정수 입력 : 11
//두 수 모두 홀수이다
728x90
반응형
'Dev > Java' 카테고리의 다른 글
[JAVA기초(8)] 조건문 실습(1), (2) (0) | 2022.05.07 |
---|---|
[JAVA기초(7)] 조건문 - if문, switch문 (0) | 2022.05.07 |
[JAVA기초(5)] 연산자 (0) | 2022.05.07 |
[JAVA기초(4)] 실습(3),(4) (0) | 2022.05.07 |
[JAVA기초(3)] 상수, 데이터 오버플로우, 형변환 (0) | 2022.05.07 |