一、明确任务
一个学生进行自我介绍的案例,在控制台输出:我叫***,男/女,我上学期通过努力,取得了好成绩,各门功课的成绩如下:C语言85,高数89,线代87,思想品德与修养89。
要求: 1、姓名、性别和各门功课的分数从键盘接收; 2、课程名称直接赋初值初始化; 运行结果如下图:
二、分析设计——所用知识点:数组
OOA:
类:学生
成员变量(描述属性):姓名、上学期的课程、各门课的成绩
成员方法(描述行为):数据初始化、自我介绍
OOD:
Student |
name:string course:String[] score:float[] |
+init():void +speak():void |
TestStudentSpeak |
+main(String[]):void |
三、源代码
import java.util.Scanner;
public class TestStudentSpeak{
public static void main(String[] args){
Student stu = new Student();
stu.init();
stu.speak();
}
}
class Student{
String name;
String[] course = {"Course1","Course2","Course3","Course4"};
float[] score;
public void init(){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the student's name -> ");
name = sc.next();
System.out.println("Please enter the course's score -> ");
score = new float[course.length];
for(int i = 0; i < course.length; i++){
System.out.println(course[i]);
score[i] = sc.nextFloat();
}
}
public void speak(){
System.out.println("My name is" + name + "My courses' are ->");
for(int i = 0; i < course.length; i++){
System.out.println(course[i] + "->" + score[i] + ";");
}
}
}