[CodeUp]C言語基礎100題(1061~1070)-Java


✔ 1061
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);
    }
}
✔ 1062
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✔ 1063
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);
    }
}
3つの演算子を使用して大きな数を出力します.3つの演算子を使用する場合は、前に変数を指定します.
✔ 1064
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);
    }
}
✔ 1065
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);
    }
}
✔ 1066
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");
            }
        }
    }
}
✔ 1067
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");
        }
    }
}
✔ 1068
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");
        }
    }
}
✔ 1069
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?");
        }
    }
}
JDK 7の前に、switchの次の括弧に整数タイプの変数のみが表示されます.ただし、JDK 7から文字列タイプの変数が次の括弧switchに現れることもあります.
✔ 1070
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にダイヤルしない場合は、次のコマンドが実行されます.このように書くと、3つのケースが縛られ、共通の単語が出力されます.