人和圆的面积
先创建对象
输出题目要求的形状
在方法里定义了方法
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;
}
}
}
}
}