【问题标题】:Java Looping QuizzesJava 循环测验
【发布时间】:2013-09-11 11:48:56
【问题描述】:

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

所以这是代码: 导入 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.. 我希望你能帮助我,我的项目需要这个。

谢谢!

【问题讨论】:

  • 哦!谢谢大家,我现在修好了:)

标签: loops average


【解决方案1】:

问题在于变量总和。您必须在平均计算后重置它

【讨论】:

    【解决方案2】:

    在这一行之前放一个 sum=0

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

    重置每个学生的值

    【讨论】:

      【解决方案3】:

      你应该指定 sum=0;前 for (int y=1; y

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        • 2019-03-02
        • 2011-11-01
        • 2018-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多