Java(五、类和对象中的例题)
一、方法中的参数为数值型的(int)
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(); } }
运行图:
二、方法中的参数为字符串数组的(String[])
import java.util.Arrays; public class SortName { public String[] sortName(String[] unOrderArr){ Arrays.sort(unOrderArr); return unOrderArr; } }
import java.util.Arrays; import java.util.Scanner; public class SortNameTest { public static void main(String[] args) { String name[] = new String[5]; Scanner scanner = new Scanner(System.in); System.out.println("请输入学员名单:"); for (int i = 0; i < 5; i++) { name[i] = scanner.next(); } SortName sortName = new SortName(); String[] orderArr = sortName.sortName(name); System.out.println(Arrays.toString(orderArr)); scanner.close(); } }
运行图:
三、方法中的参数为对象的(Student student)
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(); } }
运行图:
四、方法中的参数为对象数组的(Student[] s)
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(); } }
运行图: