java期末考试大题复习

简单java实体类

步骤:

  1. 成员属性
  2. 无参有参构造函数
  3. getter and setter
  4. 成员方法
public class Student {
   
    // todo 成员属性
    private String name;
    private int age;
    private String gender;

    // todo 构造函数
    // 无参构造
    public Student() {
   
    }

    // 有参构造
    public Student(String name, int age, String gender) {
   
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    // todo getter & setter
    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public int getAge() {
   
        return age;
    }

    public void setAge(int age) {
   
        this.age = age;
    }

    public String getGender() {
   
        return gender;
    }

    public void setGender(String gender) {
   
        this.gender = gender;
    }

    // todo 成员方法
    @Override
    public String toString() {
   
        return "Student [age=" + age + ", gender=" + gender + ", name=" + name + "]";
    }
    
}

输入流

Scanner sc = new Scanner(System.in);

读取键盘输入

nextLine()

nextLine()可用于除整型之外的读取其他格式的

nextInt()

nextInt()可用于整型数据的读取

对象数组

某次软件设计竞赛有5个队伍参赛,比赛中,每个队伍需要说明作品名称、队员人数和阐述自己的作品。为此,测试类命名为:SoftwareCompetition,队伍类名为:Team,它具有私有成员数据:作品名称name,队员人数numberofMember,作品说明introduction,和相应的get和set方法。

测试类的作用是:创建具有5个元素的Team数组,人数依次为1,2,3,4,5,请利用循环结构和Scanner对象,进行相应的Team的信息输入,最后输出各队信息。

实体类

public class Team {
   
    private String name;
    private int numberOfMember;
    private String introduction;
    public Team(){
   

    }
    public Team(String name, int numberOfMember, String introduction) {
   
        this.name = name;
        this.numberOfMember = numberOfMember;
        this.introduction = introduction;
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public int getNumberOfMember() {
   
        return numberOfMember;
    }

    public void setNumberOfMember(int numberOfMember) {
   
        this.numberOfMember = numberOfMember;
    }

    public String getIntroduction() {
   
        return introduction;
    }

    public void setIntroduction(String introduction) {
   
        this.introduction = introduction;
    }
}

测试类

import java.util.Scanner;

public class SoftwareCompetition {
   
    public static void main(String[] args) {
   
        Team[] teams = new Team[5];
        // todo 声明输入流 单个输入流可能因为类型问题无法正常输入
        Scanner str = new Scanner(System.in);
        Scanner num = new Scanner(System.in);
        // todo 循环遍历赋值 
        for (int i = 0
  • 25
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值