[20-05-01][Self-test 28]Java Students' Score

2021-02-01 16:14

阅读:665

标签:new   print   编写   结果   avg   计算   java   package   ack   

 1 package test_6_2;
 2 
 3 public class Student {
 4 
 5     /**
 6      * 编写一个程序,已有若干学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。
 7      */
 8     
 9     public static int stuNum;
10     public static double totalScore;
11     
12     public String code;
13     public String name;
14     public double score;
15     
16     public Student() {
17         
18     }
19     
20     public Student(String code, String name, double score) {
21         
22         this.code = code;
23         this.name = name;
24         this.score = score;
25         
26         System.out.println("学号:" + this.code + ",姓名:" + this.name + ",成绩:" + this.score);
27         
28         stuNum++;
29         totalScore += score;
30     }
31     
32     public void avg() {
33         
34         System.out.println("平均分是:" + totalScore / stuNum);
35     }
36     
37 }

 

 1 package test_6_2;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6 
 7         Student s1 = new Student("01", "Joker", 90);
 8         Student s2 = new Student("02", "Navi", 100);
 9         Student s3 = new Student("03", "Skull", 20);
10         Student s4 = new Student("04", "Violet", 90);
11         
12         s1.avg();
13 
14     }
15 
16 }

 

结果如下:

学号:01,姓名:Joker,成绩:90.0
学号:02,姓名:Navi,成绩:100.0
学号:03,姓名:Skull,成绩:20.0
学号:04,姓名:Violet,成绩:90.0
平均分是:75.0

[20-05-01][Self-test 28]Java Students' Score

标签:new   print   编写   结果   avg   计算   java   package   ack   

原文地址:https://www.cnblogs.com/mirai3usi9/p/12813294.html


评论


亲,登录后才可以留言!