练习,面向对象

人和圆的面积

先创建对象

输出题目要求的形状

在方法里定义了方法

Illegal modifier for parameter method; only final is permitted 出现这样的错误

调用方法都要对象,除非static的方法

对象数组

对象数组的创建

Stu[] test= new Stu[20];

主类也需要一个对象

Student student = new Student();// 对象创建

随机数取整

原因,radom的范围小于一,不能直接取整
(int) (Math.random() * (6 - 1 + 1) + 1);

对象数组需要两次new

有点像二维数组,只有这样才能存属性
Stu[] test = new Stu[20];
test[i]=new Stu();

注意返回值,注意形参别像c++那样,写一样的

public int searchState(Stu[] test, int state)

看清楚代码块,写到循环里了

public int searchState(Stu[] test, int state) { // 函数类型
for (int i = 0; i < test.length; i++) {
if (test[i].state == state) {
return i;
}
}
return -1; // 放在错误的代码块里
}

比较成绩,交换对象

public void sort(Stu[] test) {
for (int i = 0; i < test.length - 1; i++) {
for (int j = 0; j < test.length - 1 - i; j++) {
if (test[j].score > test[j + 1].score) {
Stu temp = test[j];
test[j] = test[j + 1];
test[j + 1] = temp;
}
}
}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值