计算类:public class ScoreCalc {
double avg;
double jscore;
double cscore;
double dbScore;
double sum;
int stuNo;
int count; /*
* Calculate the total score
*/
public double sumCalc() {
return sum = jscore+cscore+dbScore;
} /*
* Calculating 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();
}
}测试类:
import java.util.Scanner;public class TestScoreCalc { /**
* @requests:Test the function of calculate average, and the total score to
* achieve
*/
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值出错?
double avg;
double jscore;
double cscore;
double dbScore;
double sum;
int stuNo;
int count; /*
* Calculate the total score
*/
public double sumCalc() {
return sum = jscore+cscore+dbScore;
} /*
* Calculating 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();
}
}测试类:
import java.util.Scanner;public class TestScoreCalc { /**
* @requests:Test the function of calculate average, and the total score to
* achieve
*/
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值出错?
System.out.println(s.showSum() + "\n" + s.showAvg());看你的意图这句是想要统计全部学生的总分和平均分?
但由于你前后所有个学生用的是同一个ScoreCalc对象,因此当执行到这里的时候,除了最后一个学生,前面所有学生的信息早已经被覆盖从而丢失了,因此你这句应该只能输出最后一个学生的总分和平均分。