package test;
public class Person {
public static void main(String[] args) {
Method m =new Method();
//打印3*8的矩阵
m.count(3,8);
//计算矩阵面积
System.out.println("面积为:"+m.square(3,8));
}
}
class Method{
public void count(int a, int b){
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
System.out.print("*");
}
System.out.println();
}
}
public int square(int a,int b){
int s = a*b;
return s;
}
}
输出:
package test;
public class Person {
public static void main(String[] args) {
Student[] student = new Student[20];
for (int i = 0; i <student.length ; i++) {
//给数组元素赋值
student[i] = new Student();
student[i].number = i+1;
//年级【1,6】
student[i].state = (int) (Math.random()*(6-1+1)+1);
//成绩【0,100】
student[i].score = (int) (Math.random()*(100 - 0 + 1));
}
//遍历学生数组
for (int i = 0; i < student.length; i++) {
System.out.println(student[i].info());
}
Student question = new Student();
System.out.println("******************************");
//问题一:打印出三年级的学生信息
question.q1(student);
System.out.println("******************************");
//问题二:使用冒泡排序对学生成绩排序
question.q2(student);
}
}
class Student{
int number;//学号
int state;//年级
int score;//成绩
public String info(){
return "学号:" + number +",年级:" + state+",成绩:"+score;
}
public void q1(Student[] student){
for (int i = 0; i <student.length ; i++) {
if (student[i].state==3){
System.out.println(student[i].info());
}
}
}
public void q2(Student[] student){
for (int i = 0; i <student.length-1 ; i++) {
for (int j = 0; j <student.length-i-1 ; j++){
if(student[j].score>student[j+1].score){
Student temp =student[j];
student[j]=student[j+1];
student[j+1]=temp;
}
}
}
//遍历学生数组
for (int i = 0; i <student.length ; i++) {
System.out.println(student[i].info());
}
}
}