落書きノート

ふと自分が気になった事を書いてます

Javaで不定期練習

解きながら学ぶJava 入門編

解きながら学ぶJava 入門編

// 問題6-21 別解

import java.util.Scanner;

class q6_21_another {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("クラス数 : ");
        int class_num = scan.nextInt();
        int[][] point = new int[class_num][];
        int ninzu = 0;
        for(int i = 0; i < point.length; i++) {
            System.out.printf("\n%d組の人数 : ", i + 1);
            int num = scan.nextInt();
            point[i] = new int[num];
            ninzu += num;
            for(int j = 0; j < point[i].length; j++) {
                System.out.printf("%d組%d番の点数 : ", i + 1, j + 1);
                point[i][j] = scan.nextInt();
            }
        }

        System.out.println("  組 |    合計    平均");
        System.out.println("-----+----------------");
        int total = 0;
        for(int i = 0; i < point.length; i++) {
            int sum = 0;
            for(int j = 0; j < point[i].length; j++) {
                sum += point[i][j];
            }
            total += sum;
            System.out.printf("%2d組 | %7d %7.1f\n", i + 1, sum, (double)sum / point[i].length);
        }
        System.out.println("-----+----------------");
        System.out.printf("  計 | %7d %7.1f\n", total, (double)total / ninzu);
    }
}

// 問題7-1

import java.util.Scanner;

class q7_1 {
    public static int signOf(int n) {
        return n > 0 ? 1 : n == 0 ? 0 : -1;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("整数x : ");
        int answer = scan.nextInt();
        if(signOf(answer) == 1)
            System.out.println("signOf(x)は1です。");
        else if(signOf(answer) == 0)
            System.out.println("signOf(x)は0です。");
        else
            System.out.println("signOf(x)は-1です。");
    }
}

// 問題7-2

import java.util.Scanner;

class q7_2 {
    public static int min(int a, int b, int c) {
        int min = a;
        if(min > b) min = b;
        if(min > c) min = c;
        return min;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("整数a : ");
        int a = scan.nextInt();
        System.out.print("整数b : ");
        int b = scan.nextInt();
        System.out.print("整数c : ");
        int c = scan.nextInt();
        System.out.println("最小値は" + min(a, b, c) + "です。");
    }
}

// 問題7-3

import java.util.Scanner;

class q7_3 {
    public static int med(int a, int b, int c) {
        int answer;
        if(b >= a && a >= c || c >= a && a >= b)
            answer = a;
        else if(a >= b && b >= c || c >= b && b >= a)
            answer = b;
        else
            answer = c;
        return answer;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("整数a : ");
        int a = scan.nextInt();
        System.out.print("整数b : ");
        int b = scan.nextInt();
        System.out.print("整数c : ");
        int c = scan.nextInt();
        System.out.println("中央値は" + med(a, b, c) + "です。");
    }
}

// 問題7-3 別解

import java.util.Scanner;

class q7_3_another {
    public static int med(int a, int b, int c) {
        if(a >= b)
            if(b >= c)
                return b;
            else if(a <= c)
                return a;
            else
                return c;
        else if(a > c)
            return a;
        else if(b > c)
            return c;
        else
            return b;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("整数a : ");
        int a = scan.nextInt();
        System.out.print("整数b : ");
        int b = scan.nextInt();
        System.out.print("整数c : ");
        int c = scan.nextInt();
        System.out.println("中央値は" + med(a, b, c) + "です。");
    }
}

ああー、今日は疲れた…。