码迷,mamicode.com
首页 > 编程语言 > 详细

Java(五、类和对象中的例题)

时间:2017-08-21 20:40:16      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:运行   例题   print   length   zhang   log   pre   sort   his   

一、方法中的参数为数值型的(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(五、类和对象中的例题)

标签:运行   例题   print   length   zhang   log   pre   sort   his   

原文地址:http://www.cnblogs.com/ytsbk/p/7406209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!