JAVA简单编程:教师统计学生分数并输出///: Filename: Teacher.java
//老师类
public class Teacher {
//根据学生成绩的平均分由高到低排序;
private void sortStudents(Student[] s){
Student t;
for(int i=0; is.length-1; i)
if(s[i 1].getAvgResult()s[i].getAvgResult()){
t=s[i];
s[i]=s[i 1];
s[i 1]=t;
i-=i==0?1:2;
}
}
//公布学生考试成绩
private void printInfo(Student[] s){
for(int i=0; is.length; i)
System.out.println(s[i].getCls() ": " s[i].getName() " 同学,\n总分: " s[i].getTotalResult() ", 平均分:" s[i].getAvgResult() " "
" 名列本次考试的第 " (i 1) " 名."
"\n\t数学: " s[i].getResult()[0] " 分"
"\n\t语文: " s[i].getResult()[1] " 分"
"\n\t体育: " s[i].getResult()[2] " 分" "\n");
}
public static void main(String[] args) {
String cls = "三(6)班";
Student 张三 = new Student("张三",cls,new float[]{80,70,90,});
Student 李四 = new Student("李四",cls,new float[]{90,75,65,});
Student 王五 = new Student("王五",cls,new float[]{85,95,70,});
Student[] all = {张三,李四,王五,};//全部学生放入一个数组,以便分析
Teacher th = new Teacher();//创建老师类
th.sortStudents(all);//老师对学生进行排序
th.printInfo(all);//老师公布学生的结果
}
}
//学生类
class Student{
Student(){}
Student(String name,String cls,float[] r){
this.setName(name);
this.setCls(cls);
this.setResult(r);
}
private String name;//姓名
private String cls;//班级
private float[] results;//成绩
//SETTER
public void setName(String s){name=s;}
public void setCls(String s){cls=s;}
public void setResult(float[] f){results=f;}
//GETTER
public String getName(){return name;}
public String getCls(){return cls;}
public float[] getResult(){return results;}
public float getTotalResult(){//获取学生总成绩
float f = 0;
for(int i=0; iresults.length; i)
f =results[i];
return f;
}
public float getAvgResult(){//获取学生平均成绩
return getTotalResult()/results.length;
}
}
用java学生成绩统计import java.util.Scanner;
public class Test {
protected static boolean end_flag = true;
public static void main(String[] args) {
Students student = new Students();
student.InputData();// 输入学生数据
String order;
while (end_flag) {
System.out.println("请输入数字1-5:");
Scanner s = new Scanner(System.in);
order = s.nextLine();
student.Operator(order);
}
}
}
Students类:
import java.util.ArrayList;
import java.util.Scanner;
public class Students {
private int STUDENTS_NUM = 5; // 学生的人数
private int SUBJECTS_NUM = 3; // 课程数
private String[][] data = https://www.04ip.com/post/new String[STUDENTS_NUM][SUBJECTS_NUM2];
private String name;
private int Chinese_score;
private int Math_score;
private int English_score;
public void SearchHighestScore(String[][] original) { // 查找最高成绩
int flag = Integer.parseInt(original[0][4]), position = 0;
for (int i = 1; ioriginal.length; i) {
if (Integer.parseInt(original[i][4])flag) {
flag = Integer.parseInt(original[i][4]);
position = i;
}
}
System.out.print("最高分同学的姓名和总分为:");
System.out.println(original[position][0]" "original[position][4]);
}
public void SearchLowestScore(String[][] original) {
int flag = Integer.parseInt(original[0][4]), position = 0;
for (int i = 1; ioriginal.length; i) {
if (Integer.parseInt(original[i][4])flag) {
flag = Integer.parseInt(original[i][4]);
position = i;
}
}
System.out.print("最低分同学的姓名和总分为:");
System.out.println(original[position][0]" "original[position][4]);
}
public void OrderStudents(String[][] original) {
String[][] result = original;
String temp;
for (int i = 1; iresult.length; i) {
for (int j = 0; jresult.length - i; j) {
if (Integer.parseInt(result[j][4])Integer
.parseInt(result[j1][4])) {
temp = result[j][4];
result[j][4] = result[j1][4];
result[j1][4] = temp;
temp = result[j][0];
result[j][0] = result[j1][0];
result[j1][0] = temp;
}
}
}
System.out.println("排名\t""姓名\t""总分\t");
for (int i = 0; iSTUDENTS_NUM; i) {
System.out.println(i1"\t"result[i][0]"\t"
result[i][4]);
}
}
public void ScoreUnder60(String[][] original) {
System.out.println("平均分在60以下的学生的姓名为:");
ArrayList temp = new ArrayList();
for (int i = 0; ioriginal.length; i) {
if (Integer.parseInt(original[i][4]) / SUBJECTS_NUM60) {
temp.add(original[i][0]);
}
}
System.out.print(temp" ");
System.out.println("\n""平均分在60以下的学生的人数为:"
temp.size());
}
public Integer SearchScore(String student_name) {
int position = -1;
for (int i = 0; idata.length; i) {
if (student_name.equals(data[i][0])) {
position = i;
}
}
if (position != -1)
return Integer.valueOf(data[position][4]);
else
return null;
}
public void Operator(String order) {
if ("1".equals(order)) {
SearchHighestScore(data);
SearchLowestScore(data);
} else if ("2".equals(order)) {
OrderStudents(data);
} else if ("3".equals(order)) {
ScoreUnder60(data);
} else if ("4".equals(order)) {
String name;
System.out.println("请输入学生的姓名:");
Scanner s = new Scanner(System.in);
name = s.nextLine();
if (SearchScore(name) != null)
System.out.println(name"的总分为:"SearchScore(name));
else
System.out.println("查无此人");
} else if ("5".equals(order)) {
Test obj = new Test();
obj.end_flag = false;
System.out.println("退出");
} else {
【java统计数学成绩代码 java统计学生成绩】System.out.println("输入有误!");
}
}
public void InputData() {
for (int i = 1; i = STUDENTS_NUM; i) {
System.out.print("请输入第"i"个人的姓名"":");
Scanner s1 = new Scanner(System.in);
name = s1.nextLine();
data[i - 1][0] = name;
System.out.print("请输入第"i"个人的语文成绩"":");
Scanner s2 = new Scanner(System.in);
Chinese_score = s2.nextInt();
data[i - 1][1] = String.valueOf(Chinese_score);
System.out.print("请输入第"i"个人的数学成绩"":");
Scanner s3 = new Scanner(System.in);
Math_score = s3.nextInt();
data[i - 1][2] = String.valueOf(Math_score);
System.out.print("请输入第"i"个人的英语成绩"":");
Scanner s4 = new Scanner(System.in);
English_score = s4.nextInt();
data[i - 1][3] = String.valueOf(English_score);
data[i - 1][4] = String.valueOf(Chinese_scoreMath_score
English_score);
System.out.println(data[i - 1][4]);
}
}
}
java设有一个数组score,内放10名学生的成绩,写一个程序计算平均分、最高分、最低分 。这是代码java统计数学成绩代码,其中每行main函数java统计数学成绩代码的要点代码我都作了注释,希望你能理解这个程序:
package example;
import java.util.Scanner;
public class example
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in); //声明Scanner对象in,用于读取输入
float[] score = new float[10]; //声明score数组,用于存储分数
float max, min; //声明变量max与min,用于存储最高分与最低分
float average = 0.0f; //声明变量average(初始化为0.0),用于存储平均值
System.out.println("请输入10个同学的成绩:"); //输出提示信息
for (int i = 0; i10; i) //声明for循环,用于遍历score数组
{
score[i] = in.nextFloat(); //读取分数到score数组相应位置
}
max = score[0]; //假设最大值为score[0]
min = score[0]; //假设最小值为score[0]
average= score[0]; //average加上score[0]以便后面的平均值计算
for (int i = 1; i10; i) //声明for循环 , 用于遍历score数组
{
if (score[i]max) //如果score[i]大于max值(即找到更大的值)
{
max = score[i]; //重新赋值给max
}
else if (score[i]min) //如果score[i]小于min值(即找到更小的值)
{
min = score[i]; //重新赋值给min
}
average= score[i]; //average加上score[0]以便后面的平均值计算
}
average /= 10.0; //计算平均分
System.out.print("平均分:"average" 最高分:"max" 最低分:"min); //输出结果
in.close();//关闭in流
}
}
希望能帮到您,有什么不懂的可以再问我 。
java编程,三十名同学数学成绩输入一堆数组中,求平均分,求总分,求最高分和最低分非常推荐用Java8的新特性Stream来解决这类求数据统计结果的java统计数学成绩代码,真的很方便java统计数学成绩代码,代码简洁而优雅
用到java统计数学成绩代码了IntSummaryStatistics类,这个类就包含了题主说的各种统计结果了
ListStudent list = Arrays.asList(new Student(100), new Student(59), new Student(80), new Student(92));
IntSummaryStatistics summaryStatistics = list.stream().mapToInt(Student::getScore).summaryStatistics();
System.out.println("最高分java统计数学成绩代码:"summaryStatistics.getMax());
System.out.println("最低分java统计数学成绩代码:"summaryStatistics.getMin());
System.out.println("总分:"summaryStatistics.getSum());
System.out.println("平均分:"summaryStatistics.getAverage());
可以参考了解一下
JAVA代码问题!输入5名学生的成绩,并计算总成绩,平均分,最高分,最低分public static void main(String[] args) {
double scores[] = new double[5];
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=0;
String inputStr=null;
System.out.println("请输入5名学生java统计数学成绩代码的成绩:");
Scanner input = new Scanner(System.in);
while(count5){
try{
if(count5){
System.out.println("请输入第" (count 1) "个分数:");
}
inputStr=input.nextLine();
scores[count]=Double.valueOf(inputStr.trim());
}catch(Exception e){
if(inputStr!=null"exit".equals(inputStr.trim())){
System.out.println("您已成功结束程序");
System.exit(0);
}
System.out.println("若想结束请输入:exit");
System.out.print("您输入的分数不是数值类型java统计数学成绩代码,");
count--;
}
}
input.close();
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total= score;
}
avg=total/scores.length;
System.out.println("总成绩是"total);
System.out.println("最高分是"max);
System.out.println("最低分是"min);
System.out.println("平均分是"avg);
}
//-------------------------------------------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
Double[] scores = null;
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=1;
ListDouble inputScores=new ArrayListDouble();
String inputStr=null;
System.out.println("请输入要统计学生的成绩(理论上可以输入无限个java统计数学成绩代码,前提是你有那么大的内存):");
while(true){
try{
System.out.println("请输入第" count"个分数java统计数学成绩代码,或输入ok进行计算,离开请输入exit");
inputStr=input.nextLine();
inputScores.add((double)Double.valueOf(inputStr.trim()));
}catch(Exception e){
if(inputStr!=null"exit".equals(inputStr.trim().toLowerCase())){
System.out.println("您已成功结束程序");
input.close();
System.exit(0);
}
if(inputStr!=null"ok".equals(inputStr.trim().toLowerCase())){
break;
}
System.out.println("您输入的分数不是数值类型,");
System.out.println("若想结束请输入exit , 若想计算结果请输入ok");
count--;
}
}
if(inputScores.size()==0){
System.out.println("您没有输入学生成绩,无数据可统计 , 程序结束 。");
return ;
}
scores=inputScores.toArray(new Double[inputScores.size()]);
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total= score;
}
avg=total/scores.length;
System.out.println("总成绩是"total);
System.out.println("最高分是"max);
System.out.println("最低分是"min);
System.out.println("平均分是"avg);
}
}
java统计数学成绩代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java统计学生成绩、java统计数学成绩代码的信息别忘了在本站进行查找喔 。
推荐阅读
- java代码混乱怎么办,java代码容易出现的问题
- 小程序怎么建公众号,小程序添加公众号
- 飞行球游戏,飞行球视频
- 按任意键继续代码java bat按任意键继续代码
- 轻薄笔记本显卡怎么换主板,轻薄笔记本显卡怎么换主板的
- sap采购需要哪些条件,sap 采购流程
- 虚拟主机软件安装,虚拟主机文件怎么下载
- vb.net设置纸张打印 vba设置打印纸张类型
- 如何拍摄营销短视频赚钱,如何拍摄营销短视频赚钱呢