import java.util.Scanner;
public class ScoreCalc {
public void calc(int num1,int num2,int num3){
int sum = num1 + num2 +num3;
double avg = sum / 3;
System.out.println("总成绩:"+sum);
System.out.println("平均分:"+avg);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入Java的成绩:");
int num1 = input.nextInt();
System.out.println("请输入C的成绩:");
int num2 = input.nextInt();
System.out.println("请输入DB的成绩:");
int num3 = input.nextInt();
ScoreCalc calc = new ScoreCalc();
calc.calc(num1, num2, num3);
input.close();
}
}
public class Student {
double chinese;
double math;
double english;
public Student(double chinese,double math,double english){
this.chinese = chinese;
this.math = math;
this.english = english;
}
}
public class StudentCalc {
public double calc(Student zhangSan){
double avg = (zhangSan.chinese + zhangSan.math + zhangSan.english) / 3;
return avg;
}
}
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入语文成绩:");
double chinese = scanner.nextDouble();
System.out.println("请输入数学成绩:");
double math = scanner.nextDouble();
System.out.println("请输入英语成绩:");
double english = scanner.nextDouble();
Student zhangSan = new Student(chinese, math,english);
StudentCalc scoreAvg = new StudentCalc();
double avg = scoreAvg.calc(zhangSan);
System.out.println("三门课的平均分数是:"+avg);
scanner.close();
}
}
public class Student {
String name;
int age;
double height;
public Student(String name,int age,double height){
this.name = name;
this.age = age;
this.height = height;
}
}
public class StudentManager {
public double heightAvg(Student[] s){
double sum = 0;
for (int i = 0; i < s.length; i++) {
sum += s[i].height;
}
return sum/s.length;
}
}
import java.util.Scanner;
public class StudentManagerTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student students[] = new Student[2];
for (int i = 0; i < students.length; i++) {
System.out.println("请输入第"+(i+1)+"个学员的姓名:");
String name = scanner.next();
System.out.println("请输入第"+(i+1)+"个学员的年龄:");
int age = scanner.nextInt();
System.out.println("请输入第"+(i+1)+"个学员的身高:");
double height = scanner.nextDouble();
Student student = new Student(name,age,height);
students[i] = student;
}
StudentManager manager = new StudentManager();
double avg = manager.heightAvg(students);
System.out.println("平均身高为:"+avg);
scanner.close();
}
}
运行图: