質問編集履歴

1

エラーの修正

2020/07/08 06:04

投稿

CarpeDiemnosiru
CarpeDiemnosiru

スコア4

test CHANGED
@@ -1 +1 @@
1
- NullPointerExceptionの解決方法を教えてください。
1
+ ArrayIndexOutOfBoundsExceptionの解決方法を教えてください。
test CHANGED
@@ -18,9 +18,11 @@
18
18
 
19
19
  ```
20
20
 
21
- Exception in thread "main" java.lang.NullPointerException
21
+ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
22
22
 
23
- at hello.Seiseki1.main(Seiseki1.java:12)
23
+ at hello.Seiseki1.main(Seiseki1.java:14)
24
+
25
+
24
26
 
25
27
  ```
26
28
 
@@ -40,19 +42,23 @@
40
42
 
41
43
 
42
44
 
43
- private static String[] names;
44
45
 
45
- private static double[][] score;
46
-
47
-
48
46
 
49
47
  public static void main(String[] args) {
50
48
 
49
+ int i;
50
+
51
51
  int total = 0;
52
+
53
+ int n = args.length/2;
54
+
55
+ String names[] = new String[n];
56
+
57
+ double score[][] = new double[n][2];
52
58
 
53
59
 
54
60
 
55
- for(int i = 0; i < args.length; i += 2 ) {
61
+ for(i = 0; i <args.length; i += 2 ) {
56
62
 
57
63
  names[i] = args[i];
58
64
 
@@ -60,53 +66,55 @@
60
66
 
61
67
  total += score[i][0];
62
68
 
63
-
69
+ }
64
70
 
65
- double average = (total/i);
71
+ double average = (total/(args.length/2));
66
72
 
67
73
  double ssum = 0.0;
68
74
 
69
75
  ssum += sqr(score[i][0] - average);
70
76
 
71
- double variance = ssum / i;
77
+ double variance = ssum / (args.length/2);
72
78
 
73
79
  double sd = Math.sqrt(variance);
74
80
 
75
81
  double hensachi = 50 + (score[i][0] - average)/sd * 10;
76
82
 
83
+
84
+
85
+
86
+
77
-
87
+ System.out.println("Average = " + average);
88
+
89
+ System.out.println("Standard Deviation = " + sd);
78
90
 
79
91
 
80
92
 
81
- for(i = 0; i < score.length; i++) {
93
+ for(i = 0; i <score.length; i++) {
82
94
 
83
95
  score[i][1] = hensachi;
84
96
 
85
97
 
86
98
 
87
- System.out.println("Average = " + average);
88
-
89
- System.out.println("Standard Deviation = " + sd);
90
-
91
99
 
92
100
 
93
-
101
+ System.out.print(" " + names[i] + " ");
94
102
 
95
- System.out.println(" " + names[i]);
103
+ System.out.print(score[i][0] + " ");
96
104
 
97
- System.out.println(score[i][0]);
105
+ System.out.println(hensachi);
98
-
99
- System.out.println(score[i][1]);
100
106
 
101
107
  }
102
108
 
103
109
 
104
110
 
111
+
112
+
105
113
  }
106
114
 
107
115
 
108
116
 
109
- }
117
+
110
118
 
111
119
 
112
120
 
@@ -128,7 +136,7 @@
128
136
 
129
137
 
130
138
 
131
- 配列nullで初期化したりしたのですが、うまくいきません。
139
+ forの条件式変えたりしたのですが、うまくいきません。
132
140
 
133
141
 
134
142