khr0404 score 43
2016/05/02 11:54 投稿
javaでアンケートの集計結果について |
現在javaでアンケートの集計をやっているのですがアンケートの結果が実際の数値と異なって出てきてしまいます。 |
原因がわからなく手詰まりになっていしまいました。 |
どこでループがおかしくなっているのか教えて頂きたいです。 |
###実行結果 |
実行したところこのような結果になります。 |
``` |
Q1. 性別は? [1 : 男 2 : 女 ]6人7人 |
Q2. 年齢は? [1 : 10代以下 2 : 20代 3 : 30代 4 : 40代 5 : 50代以上 ]3人6人5人5人6人 |
Q3. インターネットの利用頻度は? (携帯電話からの利用も含む) [1 : ほぼ毎日 2 : たまに 3 : 使ったことがある程度 4 : 使ったことがない ]6人3人5人6人 |
``` |
###アンケート実行クラス |
``` |
public class EnqueteExamMain { |
public static void main( String[] args ) { |
Enquete enq = new Enquete(); |
enq.setQuestions( makeQuestion() ); |
makeAnswer( enq ); |
enq.agg(); |
dispResult( enq ); |
} |
private static void makeAnswer( Enquete enq ) { |
int[][] ans = { |
{ |
1, |
1, |
1 |
}, |
{ |
1, |
2, |
1 |
}, |
{ |
1, |
2, |
1 |
}, |
{ |
1, |
2, |
3 |
}, |
{ |
1, |
3, |
4 |
}, |
{ |
2, |
1, |
1 |
}, |
{ |
2, |
2, |
3 |
}, |
{ |
2, |
4, |
1 |
}, |
{ |
2, |
3, |
2 |
}, |
{ |
2, |
5, |
4 |
}, |
}; |
for ( int i = 0; i < ans.length; i++ ) { |
Answer a = new Answer(); |
a.setValues( ans[i] ); |
enq.addAnswer( a ); |
} |
} |
private static Question[] makeQuestion() { |
String[] s1 = { |
"男", |
"女" |
}; |
String[] s2 = { |
"10代以下 ", |
"20代 ", |
"30代 ", |
"40代 ", |
"50代以上 " |
}; |
String[] s3 = { |
"ほぼ毎日", |
"たまに", |
"使ったことがある程度", |
"使ったことがない" |
}; |
Question[] questions = { |
new Question( "性別は?", s1 ), |
new Question( "年齢は?", s2 ), |
new Question( "インターネットの利用頻度は? (携帯電話からの利用も含む)", s3 ) |
}; |
return questions; |
} |
private static void dispResult( Enquete enq ) { |
MyResult[] r = enq.getResults(); |
for ( int i = 0; i < r.length; i++ ) { |
Question q = r[i].getQuestion(); |
System.out.print( "Q" + ( i + 1 ) + ". " + q.getQuestion() ); |
System.out.print( " [" ); |
for ( int j = 0, num = q.getLabels().length; j < num; j++ ) { |
System.out.print( ( j + 1 ) + " : " + q.getLabels()[j] + " " ); |
} |
System.out.print( "]" ); |
for ( int j = 0, num = q.getLabels().length; j < num; j++ ) { |
System.out.print( ( j + 1 ) + r[i].getCount( j ) + "人" ); |
} |
System.out.println(); |
System.err.println(); |
} |
} |
} |
``` |
###集計結果を表すクラス |
``` |
public class MyResult { |
private Question question; |
private final int[] counts; |
public MyResult( Question q ) { |
setQuestion( q ); |
this.counts = new int[q.getLabels().length]; |
} |
public void countUp( int value ) throws ArrayIndexOutOfBoundsException { |
this.counts[value]++; |
} |
public int getCount( int value ) { |
return this.counts[value]; |
} |
public Question getQuestion() { |
return this.question; |
} |
public void setQuestion( Question question ) { |
this.question = question; |
} |
} |
``` |
###アンケートの設問一つを現すクラス |
``` |
public class Question { |
private String question; |
private String[] labels; |
public Question( String question, String[] labels ) { |
setQuestion( question ); |
setLabels( labels ); |
} |
public String[] getLabels() { |
return this.labels; |
} |
public void setLabels( String[] strings ) { |
this.labels = strings; |
} |
public String getQuestion() { |
return this.question; |
} |
public void setQuestion( String string ) { |
this.question = string; |
} |
} |
``` |
###一つ回答を表すクラス |
``` |
public class Answer { |
private int[] values; |
public int[] getValues() { |
return this.values; |
} |
public void setValues( int[] is ) { |
this.values = new int[is.length]; |
for ( int i = 0; i < is.length; i++ ) { |
this.values[i] = is[i] - 1; |
} |
} |
} |
``` |
###回答オブジェクトクラス |
``` |
import java.util.ArrayList; |
import java.util.List; |
public class Enquete { |
private Question[] questions; |
private final List<Answer> answers = new ArrayList<Answer>(); |
private MyResult[] results; |
public Question[] getQuestion() { |
return this.questions; |
} |
public void setQuestions( Question[] questions ) { |
this.questions = questions; |
} |
public void addAnswer( Answer a ) { |
this.answers.add( a ); |
} |
public MyResult[] getResults() { |
return this.results; |
} |
public void agg() throws ArrayIndexOutOfBoundsException { |
results = new MyResult[questions.length]; |
for ( int i = 0; i < questions.length; i++ ) { |
results[i] = new MyResult( questions[i] ); |
for ( int j = 0; j < answers.size(); j++ ) { |
int value[] = answers.get( j ).getValues(); |
results[i].countUp( value[i] ); |
} |
} |
} |
} |
``` |
###補足情報(言語/FW/ツール等のバージョンなど) |
###補足情報(言語/FW/ツール等のバージョンなど) |
java |
eclipse |