weixin_40182907
坚持可持续
2018-04-17 14:15
采纳率: 38.5%
浏览 1.6k
已采纳

JAVA初学者练习题,求找错

package report5;

import java.util.ArrayList;




public class Student {

       int id;
       String name;
       ArrayList<Integer> quizResult =new  ArrayList<Integer>(10) ;

       public Student()
       {}
       public Student( int id){

           this.id=id;
       }


       public Student(int id, String name){
           this.id=id;
           this.name=name;
       }
       public void setID(int id){ 
         this.id=id;  
       }

       public void setName(String name){
           this.name=name;
       }
       void setAllResult(int a, int b, int c, int d, int e){
           quizResult.add(a);
           quizResult.add(b);
           quizResult.add(c);
           quizResult.add(d);
           quizResult.add(e);
       }
       void setUpdate(int index, int value){
           quizResult.set(index,value );
       }

        int sum()
        {   int sum=0; 
            for(int i:quizResult)
              sum+=quizResult.get(i)   ;
            return sum;
        }
       void print(){
        System.out.printf("%d %s ",id,name);   
        for(int i=0; i<quizResult.size() ;i++)
             System.out.printf("%d ",quizResult.get(i) ) ;
        System.out.printf("(Total %d,Average %d",sum(),sum()/5);
       }


}

测试class

package report5;

public class test {

    static void method1()
    {
        Student aaa=new Student();
    Student bbb=new Student(234567);    
    Student ccc=new Student(345678,"hongg");    
    aaa.setID(123456);
    aaa.setName("chengwuql");
    aaa.setAllResult(7, 9, 7, 9, 6);
    bbb.setName("kang");
    bbb.setAllResult(7, 5, 1, 8, 8);

    ccc.setAllResult(8, 9, 8, 4, 8);
    aaa.print();
    bbb.print();
    ccc.print();

    }

    public static void main(String[] args) {
        method1();

    }

}

题目是 使用 给定的部分代码 然后完成。。。

求 aaa bbb ccc 三人五次的成绩 和平均成绩
考察 重载的构造函数

输出结果

 123456 chengwuql 7 9 7 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 7 out-of-bounds for length 5
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)
    at java.base/java.util.Objects.checkIndex(Unknown Source)
    at java.base/java.util.ArrayList.get(Unknown Source)
    at report5.Student.sum(Student.java:47)
    at report5.Student.print(Student.java:54)
    at report5.test.method1(test.java:17)
    at report5.test.main(test.java:24)

本人不太会用arraylist ,应该是这里出问题了,谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • caozhy
    已采纳

    IndexOutOfBoundsException 下标越界
    你的数组只有5,但是你试图访问7

    for(int i:quizResult)
    sum+=quizResult.get(i) ;
    这里不对,应该是
    for(int i:quizResult)
    sum+=i;

    点赞 评论
  • caozhy

    另外,你要注意,/5这个是整除,要输出平均值,最好是/5.0
    特别要注意,这里是get(i),因为i是下标
    for(int i=0; i<quizResult.size() ;i++)
    System.out.printf("%d ",quizResult.get(i) ) ;
    这里i就是集合的迭代,所以不要get
    for(int i:quizResult)
    sum+=i;

    点赞 评论
  • u012261499
    s3578505 2018-04-18 07:37

    你的增强for循环写错了 for(int i:quizResult) 里面的i代指quizeResult里面的每一个元素 拿aaa来举例子 aaa为{7,9,7,9,6}, i第一次就代表7 ,
    但是quizReslt的size只有5 你写的quizReslt(i)就等于查找quizReslut的第8个元素 所以会出现数组越界 更改代码如下:
    public int sum() {

                int sum = 0; 
                for(Integer s:quizResult) {
                    sum+=s   ;
                }
                return sum;
            }
    

    还有就是你的print方法里面,最好在最后加上一行换行 这样结果显示出来比较清晰,建议代码如下:
    public void print(){

            System.out.printf("%d %s ",id,name);   
            for(int i=0; i<quizResult.size() ;i++)
                 System.out.printf("%d ",quizResult.get(i) ) ;
            System.out.printf("Total %d,Average %d",sum(),sum()/5);
            System.out.println("");
           }
    
                test的运行结果如下:
                123456 chengwuql 7 9 7 9 6 Total 38,Average 7
                234567 kang 7 5 1 8 8 Total 29,Average 5
                345678 hongg 8 9 8 4 8 Total 37,Average 7
    
    
                另:
                如果需要显示结果为小数,则sum()和print()方法中的代码应该为:
                           public double sum() {   
    
                double sum = 0; 
                for(Integer s:quizResult) {
                    sum+=s   ;
                }
                return sum;
            }
    
          public void print(){
    
            System.out.printf("%d %s ",id,name);   
            for(int i=0; i<quizResult.size() ;i++)
                 System.out.printf("%d ",quizResult.get(i) ) ;
            System.out.printf("Total %.2f,Average %.2f",sum(),sum()/5);
            System.out.println("");
           }
    
                运行结果如下:
    
                123456 chengwuql 7 9 7 9 6 Total 38.00,Average 7.60
                234567 kang 7 5 1 8 8 Total 29.00,Average 5.80
                345678 hongg 8 9 8 4 8 Total 37.00,Average 7.40
    
    点赞 评论

相关推荐