这个是计算类:
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();
}
}
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();
}
}
解决方案 »
- 人员管理系统 请高手进! 急求!!java se
- static域被继承的后,子类和父类一起执行,这块代码只被执行一次。为什么?
- jdom中调用setAttribute(Sring key,Sring value)时,如果key中带有冒号,则报错
- 关于return this的问题.
- JBuilder中类的java路径
- 请问各位,如何结束一个java进程?
- 关于树的代码问题
- 刚学习JBuilder,请各位推荐基本JBuilder方面的书籍!!!
- 想问一下语言的标准制定者和编译器制造商之间的关系
- 请问servlet怎么与swing通信的
- 反射出错java.lang.IllegalArgumentException: argument type mismatch
- 谁帮忙解释一下这个错误的产生 感激不尽
* @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 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 你是说那个值有问题
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
total score:应该是12才对呀 我想求的是所有学生的成绩总和。。那个average我再看看。
* @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 对象的属性不会累加的 你每给他赋值一起 前面的值就会被覆盖 所以你要保存结果才可以 谢谢啊 希望能给分
return sum += jscore+cscore+dbScore;
}
你是求每次输入完一个学生成就就求和?那这里就写错了。
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;