import java.util.Scanner; public class Test { public static void main(String[] args) { //創建一個學生對象 Student s = new Student(); //創建一個學生成績對象 StudentScore sc = new StudentScore(); //從控制台接收學生成績 Scanner input = new Scanner(System.in); //提示用戶輸入學生成績 System.out.println("please enter three score"); //定義控制台輸入的參數 s.Java = input.nextInt(); s.SQL = input.nextInt(); s.English = input.nextInt(); //調用方法 System.out.println("該同學的平均成績為:" + sc.calcAvg(s)); } }
public class Student { //定義一個學生類 int Java; int SQL; int English; }
public class StudentScore { //定義一個學生的平均成績 public double calcAvg(Student s1) { double avg = 0; avg = (s1.Java + s1.SQL + s1.English) / 3.0; return avg; } }