Created
December 25, 2016 12:38
-
-
Save mokomokohitsuzi/2e76e0af40e1349076793073c52f615e to your computer and use it in GitHub Desktop.
新・明解Java入門 演習7-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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