加深java类和对象理解的题目

问题:练习3外加找到任意年级的任意成绩,找到返回学号

实现功能的类和对象和方法:

知识点:

创建类(主要包括属性和方法);

类中的方法,只有用该类创造的对象才能够调用;

  实现功能的代码:

import java.util.Scanner;

public class StudentText {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		//创建类的对象数组,20个学生
		Student[] stus = new Student[20];
		
		//在主方法中调用其他的方法,同样要造一个对象,类的对象才能够调用方法
		StudentText text = new StudentText();
		
		//给类的对象数组的每一个元素开辟新的空间并且赋随机值
		text.offerRandomNumber(stus);
		
		//遍历对象数组
		text.printf(stus);
		
		//输出三年级学生的对象信息
		text.serchState(stus, 3);
		
		//使用冒泡排序将学生信息按照成绩来排序
		text.bubbleSort(stus);
		
		//遍历对象数组
		text.printf(stus);
		
		//找三年级学生中成绩为指定成绩的学生
		System.out.println("请输入你想找到的年级:");
		int dest = scan.nextInt();
		System.out.println("请输入你想找到的成绩:");
		int sorce = scan.nextInt();
		int isFaind = text.dichotom(stus,dest,sorce);
		if(isFaind < 0) {
			System.out.println("没有找到");
		}else{
			System.out.println("学号为:" + isFaind);
		}
	}
	
	/**
	 * 
	 * @Description 给Student类中对象数组的每一个元素开辟新的空间并且赋随机值
	 * @author HuBin
	 * @date Mar 22, 20223:52:46 PM
	 * @param stu
	 */
	public void offerRandomNumber(Student[] stus){
		for(int i = 0;i < stus.length;i++) {
			//给每个数组元素开辟空间
			 stus[i] = new Student();
			//赋值学号
			 stus[i].number = i + 1;
			//赋值随机年级[1,6]
			 stus[i].state = (int)(Math.random() * 6 + 1);
			//赋值随机成绩[0,100]
			 stus[i].score = (int)(Math.random() * 101);
		}
	}
	
	/**
	 * 
	 * @Description 遍历对象数组的存的值
	 * @author HuBin
	 * @date Mar 22, 20224:04:27 PM
	 * @param stus
	 */
	public void printf(Student[] stus) {
		for(int i = 0;i < stus.length;i++) {
			System.out.println(stus[i].showInfo());
		}
		System.out.println("************************************");
	}
	
	/**
	 * 
	 * @Description 输出指定年级学生的对象信息
	 * @author HuBin
	 * @date Mar 22, 20224:10:07 PM
	 * @param dest:特定年级	
	 */
	public void serchState(Student[] stus,int dest) {
		for(int i = 0;i < stus.length;i++){
			if(stus[i].state == dest) {
				System.out.println(stus[i].showInfo());
			}
		}
		System.out.println("************************************");
	}
	
	/**
	 * 
	 * @Description 使用冒泡排序将学生信息按照成绩来排序
	 * @author HuBin
	 * @date Mar 22, 20224:13:39 PM
	 * @param stus
	 */
	public void bubbleSort(Student[] stus) {
		for(int i = 0;i < stus.length - 1;i++) {
			for(int j = 0;j < stus.length - 1 - i;j++) {
				if(stus[j].score > stus[j+1].score) {
					//交换的是数组元素,Student类型
					Student temp = stus[j];
					stus[j] = stus[j+1];
					stus[j+1] = temp;
				}
			}
		}
	}
	
	/**
	 * 
	 * @Description 找指定年级学生中成绩为指定成绩的学生,没有找到返回负数
	 * @author HuBin
	 * @date Mar 22, 20224:18:37 PM
	 * @param stus dest:特定年级 score:特定分数
	 * @return 没有找到返回-1,找到返回学号
	 */
	public int dichotom(Student[] stus,int dest,int score){
		for(int i = 0;i < stus.length;i++) {
			if(stus[i].state == dest) {
				if(stus[i].score == score) {
					return stus[i].number;
				}
			}
		}
		return -1;
	}
	
	
}

/**
 * 
 * @Description 
 * @author Hubin Email:2294108468@qq.com
 * @version 1.0
 * @date Mar 22, 20223:48:35 PM
 */
class Student{
	//类的属性
	int number;//学号
	int state;//年级
	int score;//成绩
	
	//类的方法
	public String showInfo(){
		return "学号:" + number + ",年级:" + state + ",成绩:" + score;
	}
}

运行结果:

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北酥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值