当前位置:网站首页>方法的传递
方法的传递
2022-07-29 20:48:00 【弱冠初心】
<访问修饰符> 返回类型 <方法名>(<形式参数列表>) {
//方法的主体
}
调用带参数的方法
对象名.方法名(参数1, 参数2,……,参数n)
实例:学员管理系统
public class StudentMgr {
//属性:存放学生姓名的数组
String[] names = new String[20];
//方法一:增加学生姓名
public void addName(String name){
for (int i = 0; i < names.length; i++) {
if(names[i]==null){
names[i]=name;
break;
}
}
}
//方法2:在固定区间内,查找某个学生
//start:起始查找位置
//end:结束查找位置
//name:查找的学生姓名
public boolean searchName(int start,int end,String name){
boolean flag = false;//是否找到该学生,flase没找到
for(int i=start-1;i<end;i++){
if(name.equals(names[i])){
flag=true;
break;
}
}
return flag;
}
//方法3:显示本版学生信息
public void showNames(){
System.out.println("本班学生列表:");
for (int i = 0; i <names.length; i++) {
if(names[i]!=null){
System.out.print(names[i]+"\t");
}
}
}接下来搞个测试类:
import java.util.Scanner;
public class TextStudentMgr {
public static void main(String[] args) {
StudentMgr mgr = new StudentMgr();
Scanner input = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("请输入第"+(i+1)+"个同学的姓名:");
String name = input.next();
mgr.addName(name);
}
mgr.showNames();
//查找某个学生信息
System.out.print("\n输入要开始查找的位置:");
int start = input.nextInt();
System.out.print("请输入要结束查找的位置:");
int end = input.nextInt();
System.out.print("请输入要查找学生的名字:");
String findName = input.next();
boolean flag=mgr.searchName(start, end, findName) ;
if(flag){
System.out.println("恭喜你,查到了该生名字!");
}else{
System.out.println("对不起,该范围内没查到该学生!");
input.close();
}
}
拿走不谢
边栏推荐
猜你喜欢

Cobaltstrike和BurpSuite桌面快捷配置

南信大提出TIPCB,一个简单但有效的用于基于文本的人员搜索的基于部分的卷积baseline

全自动化机器学习建模!效果吊打初级炼丹师!

The Ministry of Human Resources and Social Security announced that "database operation administrator" has become a new occupation, and OceanBase participated in the formulation of occupational standar

惠普服务器硬盘指示灯不亮或显示蓝色

写出优雅的Kotlin代码:聊聊我认为的 “Kotlinic“

Baidu internship students late night fun: originally giant is this kind of life

促进二十一世纪创客教育的新发展
基于PaddleSpeech搭建个人语音听写服务
![LeetCode 593 有效的正方形[数学] HERODING的LeetCode之路](/img/c2/34624c9c7693ba40d0b3724c0db611.png)
LeetCode 593 有效的正方形[数学] HERODING的LeetCode之路
随机推荐
ALBERT: A Lite BERT for Self-supervised Learning of Language Representations
Setinel 原理简介
第3章业务功能开发(线索关联市场活动,动态搜索)
OAuth,JWT ,OIDC你们搞得我好乱啊
7 行代码搞崩溃 B 站,原因令人唏嘘!
干货!联邦学习中的合作均衡
940. Different subsequences II
LeetCode--单链表--146.LRU缓存
容器网络硬核技术内幕 (小结-中)
怎么实现您的个人知识库?
微信小程序 30 自定义模板和获取用户登录凭证
容器网络硬核技术内幕 (24) 知微知彰,知柔知刚 (上)
UDP协议详解
PointPillars 工程复现
The cornerstone of distributed: reliability - What a tangled web we weave
促进二十一世纪创客教育的新发展
嵌入式分享合集24
【593. Valid Square】
指定宽度截取字符串
写出优雅的Kotlin代码:聊聊我认为的 “Kotlinic“