大家好,又见面了,我是你们的朋友全栈君。
第1个回答
2017-10-07
Student.java类:public class Student {
private String name;
private String className;
private String courseName;
private int score;
public Student(){
}
public Student(String name, String className, String courseName, int score) {
this.name = name;
this.className = className;
this.courseName = courseName;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
main类:import java.util.ArrayList;
public class StudentMain {
public static void main(String[] args){
//初始化学生信息
Student student1 = new Student(“张三”, “class1”, “Java”, 85);
Student student2 = new Student(“周乐儿”, “class1”, “C#”, 79);
Student student3 = new Student(“王涛”, “class2”, “C#”, 52);
Student student4 = new Student(“李明”, “class2”, “Java”, 48);
ArrayList students = new ArrayList<>();
students.add(student1);
students.add(student2);
students.add(student3);
students.add(student4);
//(1) 请列出所有学生的信息;
System.out.println(“(1) 请列出所有学生的信息;”);
System.out.println(“姓名 班级名 课程名 考试成绩”);
for (Student student : students) {
System.out.println(student.getName() + ” ” + student.getClassName() + ” ” + student.getCourseName() + ” ” +
student.getScore());
}
//(2) 请打印class1班的总分和平均分;
System.out.println(“(2)请打印class1班的总分和平均分;”);
ArrayList class1StudentScore = new ArrayList<>();
int class1StudentTotalScore = 0;
for (Student student : students) {
if (student.getClassName().equals(“class1”)){
class1StudentTotalScore += student.getScore();
class1StudentScore.add(student.getScore());
}
}
System.out.println(“总分:” + class1StudentTotalScore);
System.out.println(“class1平均分:” + averageScore(class1StudentScore));
//(3) 请打印Java、C#课程的平均分。
System.out.println(“(3) 请打印Java、C#课程的平均分。”);
ArrayList javaStudentTotalScore = new ArrayList<>();
ArrayList cSharpStudentTotalScore = new ArrayList<>();
for (Student student : students) {
if (student.getCourseName().equals(“Java”)){
javaStudentTotalScore.add(student.getScore());
}
if (student.getCourseName().equals(“C#”)){
cSharpStudentTotalScore.add(student.getScore());
}
}
System.out.println(“java平均分:” + averageScore(javaStudentTotalScore));
System.out.println(“c#平均分:” + averageScore(cSharpStudentTotalScore));
}
//计算平均分
private static double averageScore(ArrayList list){
double average = 0.0;
for (int i = 0; i
average += list.get(i) ;
}
//考虑size为0的情况
if (list.size() == 0){
return 0.0;
}
return average/list.size();
}
}
运行结果:
追问
大佬可以给我私信发一下完整代码么?我本人新手,手打老是出错,不知道该怎么改
追答
你给我一个邮箱,私信不能发文件
本回答被提问者采纳
本回答被提问者采纳
第2个回答
2010-03-06
主方法的类:
public class DriverStudent {
public static void main(String[] args){
Student s1 = new Student();
Student s2 = new Student();
float sum1,sum2;
s1.setId(“001”);
s1.setName(“学生甲”);
s1.setScore(70.5f,75.0f,87.0f);
s2.setId(“002”);
s2.setName(“学生乙”);
s2.setScore(89.5f,61.0f,92.0f);
sum1 = s1.getSum();
sum2 = s2.getSum();
if(sum1 > sum2){
System.out.println(s1.getInfo());
}else if(sum1 < sum2){
System.out.println(s2.getInfo());
}else{
System.out.println(“The two students got same sum scores.”+s1.getSum());
System.out.println(s1.getInfo());
System.out.println(s2.getInfo());
}
}
}
———————————————————————–
学生类
public class Student {
private String id;
private String name;
private int size = 3;
private float[] score;
public Student(){
size =3;
score = new float[size];
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float[] getScore() {
return score;
}
public void setScore(float a,float b,float c) {
score[0] = a;
score[1] = b;
score[2] = c;
}
public float getSum(){
float sum = 0;
for(int i = 0; i
sum += score[i];
}
return sum;
}
public String getInfo(){
return “ID = “+ id +” Name = ” + name + ” sum =”+ this.getSum();
}
}
本回答被提问者采纳
第3个回答
2010-03-06
public class Train009
{
public static void main(String args[])
{
Student studentA=new Student();
Student studentB=new Student();
Student studentC=new Student();
studentA.name=”A”;
studentA.number=”001″;
studentA.scoreA=97;
studentA.scoreB=93;
studentA.scoreC=93;
studentB.name=”B”;
studentB.number=”002″;
studentB.scoreA=89;
studentB.scoreB=83;
studentB.scoreC=83;
double AllscoreA,AllscoreB;
AllscoreA=studentA.scoreA+studentA.scoreB+studentA.scoreC;
AllscoreB=studentB.scoreA+studentB.scoreB+studentB.scoreC;
if(AllscoreA>AllscoreB)
{
System.out.println(“name:”+studentA.name+”\t”+”number:”+studentA.number+”\t”+”all score:”+AllscoreA);
}
else
System.out.println(“name:”+studentB.name+”\t”+”number:”+studentB.number+”\t”+”all score:”+AllscoreB);
}
}
class Student
{
String name,number;
double scoreA,scoreB,scoreC;
}
本回答被提问者采纳
第4个回答
2010-03-05
//学生类
public class Student{
private String stu_id;
private String stu_name;
private float math;
private float chinese;
private float computer;
public void setStu_id(String stu_id){
this.stu_id=stu_id;
}
public String getStu_id(){
return stu_id;
}
public void setStu_name(String stu_name){
this.stu_name=stu_name;
}
public String getStu_name(){
return stu_name;
}
public void setMath(float math){
this.math=math;
}
public float getMath(){
return math;
}
public void setChinese(float chinese){
this.chinese=chinese;
}
public float getChinese(){
return chinese;
}
public void setComputer(float computer){
this.computer=computer;
}
public float getComputer(){
return computer;
}
}
//主方法的类
public class Start{
public static void main(String[] args){
Student stu1=new Student();
stu1.setStu_id(“No1”);
stu1.setStu_name(“张三”);
stu1.setMath(89.5f);
stu1.setChinese(70f);
stu1.setComputer(98f);
Student stu2=new Student();
stu2.setStu_id(“No2”);
stu2.setStu_name(“李四”);
stu2.setMath(79.5f);
stu2.setChinese(90f);
stu2.setComputer(68f);
float stu1Sum = stu1.getMath()+stu1.getChinese()+stu1.getComputer();
float stu2Sum = stu2.getMath()+stu2.getChinese()+stu2.getComputer();
if(stu1Sum > stu2Sum){
System.out.println (“总分最高分为:”+stu1Sum);
System.out.println (“学号:”+stu1.getStu_id());
System.out.println (“姓名:”+stu1.getStu_name());
System.out.println (“数学:”+stu1.getMath());
System.out.println (“语文:”+stu1.getChinese());
System.out.println (“计算机:”+stu1.getComputer());
}else if(stu1Sum < stu2Sum){
System.out.println (“总分最高分为:”+stu2Sum);
System.out.println (“学号:”+stu2.getStu_id());
System.out.println (“姓名:”+stu2.getStu_name());
System.out.println (“数学:”+stu2.getMath());
System.out.println (“语文:”+stu2.getChinese());
System.out.println (“计算机:”+stu2.getComputer());
}else{
System.out.println (“总分一样高为:”+stu1Sum);
System.out.println (“学号:”+stu1.getStu_id());
System.out.println (“姓名:”+stu1.getStu_name());
System.out.println (“数学:”+stu1.getMath());
System.out.println (“语文:”+stu1.getChinese());
System.out.println (“计算机:”+stu1.getComputer());
System.out.println (“学号:”+stu2.getStu_id());
System.out.println (“姓名:”+stu2.getStu_name());
System.out.println (“数学:”+stu2.getMath());
System.out.println (“语文:”+stu2.getChinese());
System.out.println (“计算机:”+stu2.getComputer());
}
}
}
希望对你有帮助!
本回答被提问者采纳
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/156399.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...