这个是计算类:
public class ScoreCalc {
double avg;  //平均分
double jscore;//java分数
double cscore;//c#分数
double dbScore;//DB分数
double sum;//总分
int stuNo;//要录入成绩的学生人数
        int count;//录入分数的次数 /*
 * Calculate the total score
 */
public double sumCalc() {
return sum = jscore+cscore+dbScore;
} /*
 * Calculate grade point average
 */
public double avgCalc() {
count=stuNo*3;
return avg = sum / count;
} /*
 * Shows the average score
 */
public String showAvg() {
return "average score is: " + avgCalc();
} /*
 * Shows the total score
 */
public String showSum() {
return "Total score is: " + sumCalc();
}
}

解决方案 »

  1.   

    这个是测试类:import java.util.Scanner;public class TestScoreCalc { /**
     * @requests:测试计算并显示平均分跟总分的功能是否正确实现
     */
    public static void main(String[] args) {
    ScoreCalc s = new ScoreCalc(); Scanner input = new Scanner(System.in);
    int num;// total number of students
    double avg;
    double sum = 0;
    double jScore=0;
    double cScore=0;
    double dbScore=0;
    int count; System.out.println("Please enter the total number of students:");
    num = input.nextInt(); s.stuNo = num;
    count = s.count; for (int i = 0; i < num; i++) {

    System.out.println("Please enter the "+(i+1)+" student Java score:");
    jScore = input.nextDouble();
    s.jscore = jScore;

    System.out.println("Please enter the "+(i+1)+" student C# score");
    cScore = input.nextDouble();
    s.cscore = cScore;

    System.out.println("Please enter the "+(i+1)+" student DB score");
    dbScore = input.nextDouble();
    s.dbScore = dbScore;

    System.out.println(s.sumCalc());
    }

    System.out.println(s.showSum() + "\n" + s.showAvg());
    }
    }问题是:sum传值有问题。。但是我又不知道该怎么解决求助!这段代码sum值在第一次计算时不出错,重复循环的时候sum值却出错?!!
      

  2.   

    Please enter the total number of students:
    2
    Please enter the 1 student Java score:
    1
    Please enter the 1 student C# score
    2
    Please enter the 1 student DB score
    3
    6.0
    Please enter the 2 student Java score:
    1
    Please enter the 2 student C# score
    2
    Please enter the 2 student DB score
    3
    6.0
    Total score is: 6.0
    average score is: 1.0   你是说那个值有问题
      

  3.   

    public class ScoreCalc {
        double avg;  //
        double jscore;//java
        double cscore;//c#
        double dbScore;//DB
        double sum;
        double sum_total = 0;//
        int stuNo;//
            int count;//    /*
         * Calculate the total score
         */
        public double sumCalc() {
         sum = jscore+cscore+dbScore;
         sum_total = sum_total + sum;
            return sum;
        }    /*
         * Calculate grade point average
         */
        public double avgCalc() {
            count=stuNo*3;
            return avg = sum_total / count;
        }    /*
         * Shows the average score
         */
        public String showAvg() {
            return "average score is: " + avgCalc();
        }    /*
         * Shows the total score
         */
        public String showSum() {
            return "Total score is: " + sum_total;
        }
    }ScoreCalc
      

  4.   


    total score:应该是12才对呀  我想求的是所有学生的成绩总和。。那个average我再看看。
      

  5.   

    public class TestScoreCalc {    /**
         * @requests:测试计算并显示平均分跟总分的功能是否正确实现
         */
        public static void main(String[] args) {
            ScoreCalc s = new ScoreCalc();        Scanner input = new Scanner(System.in);
            int num;// total number of students
            double avg;
            double sum = 0;
            double jScore=0;
            double cScore=0;
            double dbScore=0;
            int count;        System.out.println("Please enter the total number of students:");
            num = input.nextInt();        s.stuNo = num;
            count = s.count;        for (int i = 0; i < num; i++) {
                
                System.out.println("Please enter the "+(i+1)+" student Java score:");
                jScore = input.nextDouble();
                s.jscore = jScore;
                
                System.out.println("Please enter the "+(i+1)+" student C# score");
                cScore = input.nextDouble();
                s.cscore = cScore;
                
                System.out.println("Please enter the "+(i+1)+" student DB score");
                dbScore = input.nextDouble();
                s.dbScore = dbScore;
                
                System.out.println(s.sumCalc());
            }
            
            System.out.println(s.showSum() + "\n" + s.showAvg());
        }
    }
    如果你把你的总分结果保存下来的话 就会是12  对象的属性不会累加的 你每给他赋值一起 前面的值就会被覆盖  所以你要保存结果才可以 谢谢啊 希望能给分
      

  6.   

    把sum初始化为0public double sumCalc() {
            return sum += jscore+cscore+dbScore;
        }
      

  7.   

    你给sun初始化为0,然后取和的方法里应该写sun += jscore+cscore+dbScore;
    你是求每次输入完一个学生成就就求和?那这里就写错了。
      

  8.   


    import java.util.Scanner;public class TestScoreCalc {    /**
         * @requests:测试计算并显示平均分跟总分的功能是否正确实现
         */
        public static void main(String[] args) {
            ScoreCalc s = new ScoreCalc();        Scanner input = new Scanner(System.in);
            int num;// total number of students
            double avg;
            double sum = 0;
            double jScore=0;
            double cScore=0;
            double dbScore=0;
            int count;        System.out.println("Please enter the total number of students:");
            num = input.nextInt();        s.stuNo = num;
            count = s.count;        for (int i = 0; i < num; i++) {
                
                System.out.println("Please enter the "+(i+1)+" student Java score:");
                jScore = input.nextDouble();
                s.jscore = jScore;
                
                System.out.println("Please enter the "+(i+1)+" student C# score");
                cScore = input.nextDouble();
                s.cscore = cScore;
                
                System.out.println("Please enter the "+(i+1)+" student DB score");
                dbScore = input.nextDouble();
                s.dbScore = dbScore;
                
                System.out.println(s.sumCalc());//这里打印里s.sumCalc()已经执行了一遍求和
            }
            
            System.out.println(s.showSum() + "\n" + s.showAvg()); //这里的s.showSum()又求了一遍和
        }
    }楼主在最后一行打印应该写
    System.out.println(s.sum + "\n" + s.showAvg());
    另外楼上给你的地方也要改,取和的方法
    sun += jscore+cscore+dbScore;