4つの数値の中から2番目に大きな値を出力するという、アルゴリズムを使ったメソッドです。
しかし、最大値が2つある場合に、最大値が出力されてしまいます。
例)3,2,4,4・・・4が出力される
どのようにすれば解決できるのでしょうか?
Java
1package unit1; 2 3public class SecondValue { 4 5 public int theSecond(int a, int b, int c, int d) { 6 int seiretsu[] = new int[4]; 7 8 int max1 = max(a, b); 9 int max2 = max(c, d); 10 int min1 = min(a, b); 11 int min2 = min(c, d); 12 13 if (max1 < max2) { 14 seiretsu[2] = max(max1, min2); 15 } else if (max1 > max2) { 16 seiretsu[2] = max(min1, max2); 17 } else { 18 seiretsu[2] = max(min1, min2); 19 } 20 return seiretsu[2]; 21 } 22 23 24 private int max(int x, int y) { 25 int second=x; 26 if (x < y) { 27 return second=y; 28 } else { 29 return second=x; 30 } 31 } 32 33 private int min(int x, int y) { 34 int second=x; 35 if (x > y) { 36 return second=y; 37 } else { 38 return second=x; 39 } 40 } 41 42 43 44 public static void main(String[] args) { 45 SecondValue sv = new SecondValue(); 46 System.out.println("second of(1,2,3,4)=" + sv.theSecond(1, 2, 3, 4)); 47 System.out.println("second of(1,2,4,3)=" + sv.theSecond(1, 2, 4, 3)); 48 System.out.println("second of(1,3,4,2)=" + sv.theSecond(1, 3, 4, 2)); 49 System.out.println("second of(1,3,2,4)=" + sv.theSecond(1, 3, 2, 4)); 50 System.out.println("second of(1,4,2,3)=" + sv.theSecond(1, 4, 2, 3)); 51 System.out.println("second of(1,4,3,2)=" + sv.theSecond(1, 4, 3, 2)); 52 53 System.out.println("second of(2,3,4,1)=" + sv.theSecond(2, 3, 4, 1)); 54 System.out.println("second of(2,3,1,4)=" + sv.theSecond(2, 3, 1, 4)); 55 System.out.println("second of(2,4,1,3)=" + sv.theSecond(2, 4, 1, 3)); 56 System.out.println("second of(2,4,3,1)=" + sv.theSecond(2, 4, 3, 1)); 57 System.out.println("second of(2,1,3,4)=" + sv.theSecond(2, 1, 3, 4)); 58 System.out.println("second of(2,1,4,3)=" + sv.theSecond(2, 1, 4, 3)); 59 60 System.out.println("second of(3,4,1,2)=" + sv.theSecond(3, 4, 1, 2)); 61 System.out.println("second of(3,4,2,1)=" + sv.theSecond(3, 4, 2, 1)); 62 System.out.println("second of(3,1,2,4)=" + sv.theSecond(3, 1, 2, 4)); 63 System.out.println("second of(3,1,4,2)=" + sv.theSecond(3, 1, 4, 2)); 64 System.out.println("second of(3,2,4,1)=" + sv.theSecond(3, 2, 4, 1)); 65 System.out.println("second of(3,2,1,4)=" + sv.theSecond(3, 2, 1, 4)); 66 67 System.out.println("second of(4,1,3,2)=" + sv.theSecond(4, 1, 3, 2)); 68 System.out.println("second of(4,1,2,3)=" + sv.theSecond(4, 1, 2, 3)); 69 System.out.println("second of(4,2,3,1)=" + sv.theSecond(4, 2, 3, 1)); 70 System.out.println("second of(4,2,1,3)=" + sv.theSecond(4, 2, 1, 3)); 71 System.out.println("second of(4,3,2,1)=" + sv.theSecond(4, 3, 2, 1)); 72 System.out.println("second of(4,3,1,2)=" + sv.theSecond(4, 3, 1, 2)); 73 74 System.out.println("second of(1,2,2,3)=" + sv.theSecond(1,2,2,3)); 75 System.out.println("second of(2,3,1,2)=" + sv.theSecond(2,3,1,2)); 76 System.out.println("second of(3,2,1,2)=" + sv.theSecond(3,2,1,2)); 77 System.out.println("second of(2,3,3,4)=" + sv.theSecond(2,3,3,4)); 78 System.out.println("second of(2,3,4,4)=" + sv.theSecond(2,3,4,4)); 79 System.out.println("second of(3,4,4,4)=" + sv.theSecond(3,4,4,4)); 80 System.out.println("second of(1,1,1,1)=" + sv.theSecond(1,1,1,1)); 81 System.out.println("second of(2,2,2,2)=" + sv.theSecond(2,2,2,2)); 82 System.out.println("second of(3,3,3,3)=" + sv.theSecond(3,3,3,3)); 83 System.out.println("second of(4,4,4,4)=" + sv.theSecond(4,4,4,4)); 84 } 85} 86
回答3件
あなたの回答
tips
プレビュー