Java(类和对象中的例题)

Posted ★【金字塔】☆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
    }
}

运行图:

 

以上是关于Java(类和对象中的例题)的主要内容,如果未能解决你的问题,请参考以下文章

面向对象中继承的例题(运算方法)

JAVA编程中的类和对象

注解例题

Java类和对象-学习笔记(超级详细~~~)

Java学习例题

Javascript 中的类和对象