回答編集履歴
1
追記
test
CHANGED
@@ -43,3 +43,101 @@
|
|
43
43
|
}
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
追記
|
50
|
+
|
51
|
+
エラー解消だけでなく、全体を少し書き直してみました。
|
52
|
+
|
53
|
+
```java
|
54
|
+
|
55
|
+
import java.util.Scanner;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
public class iroiro {
|
60
|
+
|
61
|
+
public static void main(String[] args) {
|
62
|
+
|
63
|
+
Scanner stdIn = new Scanner(System.in);
|
64
|
+
|
65
|
+
System.out.println("最大値と最小値を求めます。");
|
66
|
+
|
67
|
+
System.out.println("要素数:");
|
68
|
+
|
69
|
+
int n = stdIn.nextInt();
|
70
|
+
|
71
|
+
int a[] = new int[n]; // 箱用意
|
72
|
+
|
73
|
+
for (int i = 0; i < n; i++) {
|
74
|
+
|
75
|
+
System.out.println("要素番号[" + (i + 1) + "]");
|
76
|
+
|
77
|
+
a[i] = stdIn.nextInt();
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
System.out.println("最大値は" + maxCul(a) + "です。");
|
84
|
+
|
85
|
+
System.out.println("最小値は" + minCul(a) + "です。");
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
public static int maxCul(int[] a) {
|
92
|
+
|
93
|
+
if (a.length == 0) {
|
94
|
+
|
95
|
+
throw new IllegalArgumentException("a.length == 0");
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
int max = a[0];
|
100
|
+
|
101
|
+
for (int v : a) {
|
102
|
+
|
103
|
+
if (max < v) {
|
104
|
+
|
105
|
+
max = v;
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
return max;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
public static int minCul(int[] a) {
|
118
|
+
|
119
|
+
if (a.length == 0) {
|
120
|
+
|
121
|
+
throw new IllegalArgumentException("a.length == 0");
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
int min = a[0];
|
126
|
+
|
127
|
+
for (int v : a) {
|
128
|
+
|
129
|
+
if (v < min) {
|
130
|
+
|
131
|
+
min = v;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
return min;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
```
|