Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created October 21, 2016 12:11
Show Gist options
  • Save mokomokohitsuzi/00cb4c609e0ce553e689ba91d68883ca to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/00cb4c609e0ce553e689ba91d68883ca to your computer and use it in GitHub Desktop.
新・明解Java入門 演習3-12
import java.util.Scanner;
public class En3_13 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("整数値A:");
int a = stdIn.nextInt();
System.out.print("整数値B:");
int b = stdIn.nextInt();
System.out.print("整数値C:");
int c = stdIn.nextInt();
// 最小値を求める ※if文を2回実行して判定
int min = a;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
// 最大値を求める ※if文を2回実行して判定
int max = b;
if (a > max) {
max = a;
}
if (c > max) {
max = c;
}
// 中央を求める(最大値でも最小値でも無い) ※if文を2回実行して判定
int mid = c;
if (a != max && a != min) {
mid = a;
}
if (b != max && b != min) {
mid = b;
}
System.out.println("3つの値の中央値は"+mid+"です。");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment