关于增强for循环的使用
1 class MultipleQUestion extends Question{ //多选题类 2 3 private char[] answer; //多选题答案属性 4 5 public char[] getAnswer() { 6 return answer; 7 } 8 9 public void setAnswer(char[] answer) { 10 this.answer = answer; 11 } 12 13 MultipleQUestion(char[] answer,String title,String[] options){ //多选题构造函数 14 15 super(title,options); 16 this.setTitle(title); 17 this.setOptions(options); 18 this.answer = answer; 19 20 } 21 22 @Override 23 public boolean check(char[] answer){ //多选题检查答案方法 24 Arrays.sort(answer); 25 if(this.answer.length == answer.length){ 26 for(int i =0;i<answer.length;i++){ // 此处不能用增强for循环for(int i : answer)遍历answer,i不是char类型的元素,若用for(char i : answer)遍历,则只能用来打印,不能用来做数组下标去比较 27 if(this.answer[i] != answer[i]){ 28 return false; 29 } 30 } 31 return true; 32 } 33 return false; 34 } 35 }