Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 25, 2016 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokomokohitsuzi/2e76e0af40e1349076793073c52f615e to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/2e76e0af40e1349076793073c52f615e to your computer and use it in GitHub Desktop.
新・明解Java入門 演習7-3
import java.util.Scanner;
public class En07_03 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.println("3つの整数値を入力して下さい");
System.out.print("整数a:");
int a = stdIn.nextInt();
System.out.print("整数b:");
int b = stdIn.nextInt();
System.out.print("整数c:");
int c = stdIn.nextInt();
// メソッドminの実行
int med = med(a, b, c);
System.out.printf("中央値は%dです。", med);
}
// 中央値medを求めるメソッド
static int med(int a, int b, int c) {
// 最大値・中央値・最小値の変数
int max = a;
int med = a;
int min = a;
// 最大値・最小値のif文
if (b > max) {
max = b;
} else if (b < min) {
min = b;
}
if (c > max) {
max = c;
} else if (c < min) {
min = c;
}
// 中央値のif文
if (b != max && b != min) {
med = b;
}
if (c != max && c != min) {
med = c;
}
return med;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment