0

所以我在调试这段代码时遇到了麻烦,想寻求你的帮助..

所以这是代码: import java.io.*;

公共类 PrefEx2 { public static void main(String args[])throws IOException{

    int quiz, ave, sum=0;

    BufferedReader inpt = new BufferedReader (new InputStreamReader(System.in));

    // System.out.print("Section 1");

    for (int sect=1; sect<4; sect++){
        System.out.print("Section " + sect);
    for (int x=1; x<4; x++){
        System.out.println("\n Student " + x);
     for (int y=1; y<4; y++){
        System.out.print("Quiz " + y + ": ");
        quiz=Integer.parseInt(inpt.readLine());
        sum=sum+quiz;
     }
        ave=sum/3;
        System.out.print("Average: " + ave);
        System.out.println("");
     }

  }
    System.out.println("");
}

}

我想在输出中显示的内容:

第 1 节(最多 3 节)

-学生 1

-- 测验 1:xx

-- 测验 2:xx

-- 测验 3: xx

-- 平均:xx

-学生 2

-- 测验 1:xx

-- 测验 2:xx

-- 测验 3: xx

-- 平均:xx

-学生 3

-- 测验 1:xx

-- 测验 2:xx

-- 测验 3: xx

-- 平均:xx

==============================

所以这段代码中的问题是,平均总结起来就像学生 1 的平均数是 94,然后下一个平均数是 186 而不是 92。我希望你能帮助我,我的项目需要这个。

谢谢!

4

3 回答 3

1

The problem is the variable sum. You have to reset that after avg calc

于 2013-09-11T11:58:04.973 回答
0

你应该分配 sum=0; 之前 for (int y=1; y<4; y++)

于 2013-09-11T11:59:50.963 回答
0

在此行之前放一个 sum=0

for (int y=1; y<4; y++){

重置每个学生的值

于 2013-09-11T11:54:01.820 回答