Java(五、類和對象中的例題)

來源:互聯網
上載者:User

標籤:運行   例題   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(五、類和對象中的例題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.