C言語を使用しています。学校のプログラミングの授業で、人のグループのデータを元に、最も早く生まれた人のデータをリストアップするプログラムを作っています。グループの中で最小の出生年aを求め、そのグループの中で出生年がaである人の中で、次は最初の出生月bを求め…という風に考えているのですが、以下のようなエラーが出ます。この対処法が分かりません。また、このやり方で正しい答えを出すことは出せるでしょうか?
エラーの内容
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 17 out of bounds for length 17
at Quiz8.main(Quiz8.java:33)
以下、自分が作っているプログラムです。
class Quiz8 {
public static void main(String[] args){
String[][] senbatsu = {
{"Name","BloodType","Year","Month","Day","HomeTown","Team"},
{"Sashihara Rino","O","1992","11","21","Oita","HKT48H"},
{"Kashiwagi Yuki","B","1991","7","15","Kagoshima","AKB48B"},
{"Watanabe Mayu","AB","1994","3","26","Saitama","AKB48B"},
{"Takahashi Minami","AB","1991","4","8","Tokyo","AKB48A"},
{"Matsui Jurina","B","1997","3","8","Aichi","SKE48S"},
{"Yamamoto Sayaka","B","1993","7","14","Osaka","NMB48N"},
{"Miyawaki Sakura","A","1998","3","19","Kagoshima","HKT48KIV"},
{"Miyazawa Sae","O","1990","8","13","Tokyo","SNH48SII"},
{"Shimazaki Haruka","A","1994","3","30","Saitama","AKB48A"},
{"Yokoyama Yui","B","1992","12","8","Kyoto","AKB48A"},
{"Kitahara Rie","A","1991","6","24","Aichi","NGT48"},
{"Watanabe Miyuki","B","1993","9","19","Nara","NMB48BII"},
{"Matsumura Kaori","O","1990","1","17","Saitama","SKE48KII"},
{"Takayanagi Akane","B","1991","11","29","Aichi","SKE48KII"},
{"Shibata Aya","B","1993","4","1","Aichi","SKE48E"},
{"Muto Tomu","B","1994","11","25","Tokyo","AKB48K"}
};
int i,j,k,l;
int maxv = Integer.parseInt(senbatsu[1][2]);
int maxi = 1;
int val;
for(i=1; i<senbatsu.length; i++){
val = Integer.parseInt(senbatsu[i][2]);
if( val < maxv ){
maxv = val;
maxi = i;
}
}
int a = Integer.parseInt(senbatsu[i][3]);
for(j=1; j<senbatsu.length; j++){
int x = Integer.parseInt(senbatsu[j][2]);
int y = Integer.parseInt(senbatsu[j][3]);
if ( x == maxv && y < a ){
a = y;
maxi = j;
}
}
int b = Integer.parseInt(senbatsu[j][4]);
for(k=1; k<senbatsu.length; k++){
int p = Integer.parseInt(senbatsu[k][2]);
int q = Integer.parseInt(senbatsu[k][3]);
int r = Integer.parseInt(senbatsu[k][4]);
if ( p == maxv && q == a && r < b){
b = r;
maxi = k;
}
}
System.out.print(senbatsu[maxi][0]);
for(l=1; l<senbatsu[maxi].length; l++){
System.out.print( ", "+senbatsu[maxi][l] );
}
System.out.println("");
}
}
回答1件
あなたの回答
tips
プレビュー