「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;