「int型の変数a,b,cを宣言し、それぞれ適当な整数を代入する。 a.b.cのうち最大値を求めてその値を表示してください」というコードはどうすればいいですか? 0252デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 13:58:27.88ID:rvZgOtV/0>>251 public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
System.out.println(max(max(a, b), c)); }
static int max(int x, int y) { if (x >= y) { return x; } else { return y; } } 0253デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:01:28.38ID:rvZgOtV/0 もしくはこう public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
if (a >= b) { if (a >= c) { System.out.println(a); } else { System.out.println(c); } } else { if (b >= c) { System.out.println(b); } else { System.out.println(c); } } } 0254デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:03:04.65ID:rvZgOtV/0 あるいはこう public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
System.out.println(Stream.of(a, b, c).max(Integer::compareTo).get()); } 0255デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:04:39.34ID:rvZgOtV/0 またはこう public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
System.out.println(Stream.of(a, b, c).reduce((x, y) -> x >= y ? x : y).get()); } 0256デフォルトの名無しさん (ワッチョイ 5b46-crVl)2020/05/26(火) 14:07:14.22ID:9m0COC3M0 荒らすなゴミ 0257デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:09:36.09ID:rvZgOtV/0>>256 またはこう public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
int[] xs = {a, b, c}; Arrays.sort(xs); System.out.println(xs[xs.length - 1]); } 0258デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:11:53.33ID:rvZgOtV/0>>256 またはこう public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
TreeSet<Integer> tree = new TreeSet<Integer>(); tree.add(a); tree.add(b); tree.add(c);
System.out.println(tree.last()); } 0259デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:17:23.59ID:rvZgOtV/0>>256 またはこう public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Comparator.reverseOrder()); maxHeap.add(a); maxHeap.add(b); maxHeap.add(c);
System.out.println(maxHeap.remove()); } 0260デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:19:48.13ID:rvZgOtV/0>>256 またはこう public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
int[] xs = {a, b, c}; int max = 0;
for (int i = 0; i < xs.length; i++) { if (i == 0 || xs[i] > max) { max = xs[i]; } }
System.out.println(max); } 0261デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:21:57.57ID:rvZgOtV/0>>256 またはこう public static void main(String[] args) { int a = 3; int b = 1; int c = 2;
int max = a;
if (b > max) { max = b; }
if (c > max) { max = c; }
System.out.println(max); } 0262デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:25:18.85ID:rvZgOtV/0>>253 これバグってるwごめん 0263デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:42:03.35ID:rvZgOtV/0 あれバグってなかった、天才でごめん 0264デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 14:44:26.65ID:rvZgOtV/0>>256 つまりこう public static void main(String[] args) { int a = 1; int b = 2; int c = 3;
System.out.println(a >= b ? a >= c ? a : c : b >= c ? b : c); } 0265デフォルトの名無しさん (ワッチョイ ba52-NhWw)2020/05/26(火) 14:46:23.47ID:gtoMtdIk0>>251 3枚のトランプa,b,cがあったとき、どうやって一番大きい数字のカードを見つけるかを、まずは日本語で順を追って書いてみな。 初めから効率とかきれいに書こうとか考えず、漏れなく正しく判断する手順を考える。 選んだカードの比較結果によって次に行う手順が変わるから、そういう可能性を漏れなく考えて、a,b,cがどんな組み合わせでも大丈夫な方法を見つける。 ここまででちゃんときてから、実際にJavaのコードを書き始めてみな。 0266デフォルトの名無しさん (ワッチョイ bbe6-nqGl)2020/05/26(火) 15:33:06.22ID:WdlDkKkA0>>251 参考にするのはおそらく>>261だね 0267デフォルトの名無しさん (ワッチョイ 0e68-dE7H)2020/05/26(火) 15:36:01.37ID:XukcPGMU0 そもそも質問者がどこまで出来たかの確認が先では 0268デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 15:59:31.21ID:rvZgOtV/0>>256 どこまでできたん? 0269デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 15:59:55.46ID:rvZgOtV/0 あ、ごめん、間違えた 0270デフォルトの名無しさん (ワッチョイ 7601-o1js)2020/05/26(火) 16:00:27.57ID:rvZgOtV/0>>251 どこまでできたん? 0271デフォルトの名無しさん (ワッチョイ 339f-obnW)2020/05/26(火) 16:01:01.71ID:q+hnim2U0 【悲報】まさかのMath.max(int, int)氏 これだけコードを書かれたのに存在を無視される 0272デフォルトの名無しさん (ワッチョイ 5b46-crVl)2020/05/26(火) 16:56:54.52ID:9m0COC3M0 キチガイ警報