import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a | b);
}
}
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a ^ b);
}
}
둘이 같으면 0, 다르면 1 출력한다.
XOR
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int result = (a > b) ? a : b;
System.out.println(result);
}
}
삼항연산자를 이용해 큰 수를 출력한다. 삼항연산자를 사용할 때에는 앞에 변수를 지정해줘야 한다.
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int result = (((a < b) ? a : b) < c) ? (a < b) ? a : b : c;
System.out.println(result);
}
}
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a % 2 == 0) System.out.println(a);
if(b % 2 == 0) System.out.println(b);
if(c % 2 == 0) System.out.println(c);
}
}
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String[] arr = sc.nextLine().split(" ");
for (String s : arr) {
if (Integer.parseInt(s) % 2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
}
}
}
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n > 0){
System.out.println("plus");
}else{
System.out.println("minus");
}
if(n % 2 == 0){
System.out.println("even");
}else{
System.out.println("odd");
}
}
}
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n > 89) {
System.out.println("A");
} else if (n > 69) {
System.out.println("B");
} else if (n > 39) {
System.out.println("C");
} else {
System.out.println("D");
}
}
}
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String grade = sc.nextLine();
switch(grade){
case "A":
System.out.println("best!!!");
break;
case "B":
System.out.println("good!!");
break;
case "C":
System.out.println("run!");
break;
case "D":
System.out.println("slowly~");
break;
default:
System.out.println("what?");
}
}
}
JDK7 이전에는
switch
다음 괄호안에 정수타입의 변수만 올 수 있었습니다. 그런데 JDK7 부터는switch
다음 괄호안에 문자열 타입의 변수도 올 수 있습니다 - 프로그래머스
import java.util.Scanner;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int season = sc.nextInt();
switch(season){
case 12:
case 1:
case 2:
System.out.println("winter");
break;
case 3:
case 4:
case 5:
System.out.println("spring");
break;
case 6:
case 7:
case 8:
System.out.println("summer");
break;
case 9:
case 10:
case 11:
System.out.println("fall");
break;
}
}
}
switch
문에서break
를 걸지 않으면 다음 명령이 실행된다. 이렇게 작성하면 세 케이스가 묶여서 하나의 공통 단어를 출력하게 된다.