java自定义一个GetStr的方法_第9节:Java 方法的定义和调用、Java String 类【多测师_王sir】...

题目:1、使用String里面的方法。packagecom.Keti;importjavax.swing.text.AttributeSet;importjava.util.Arrays;public classLesson {public staticvoidmain(String[] args) {

Stringstr1="233";

Stringstr2="1";//charAt的使用方法

char ii=str1.charAt(2);//索引返回的值对应就是定义值。

System.out.println("charAt的使用方法:"+ii);//compareTo 比较两个数的字符典

inti1=str2.compareTo("1");

System.out.println("compareTo的比较字符典:"+i1);//concat将指定的字符串,此字符串的末尾。

Stringi2=str1.concat("cn");//在选定的字符串,后面加上指定的字符串

System.out.println("concat将字符串放末尾:"+i2);//endsWith:测试如果这个字符串以指定的后缀结束。

Stringstr3="1,";

booleani3=str3.endsWith(",");//如果测试的字符串后缀与指定的相同,就输出true,否则输出false

System.out.println("endsWith测试字符串以指定的后缀结束:"+i3);//equalsIgnoreCase:比较这 String到另一个 String,忽略问题的考虑。

String str4="abcd";

Stringstr5="aBCd";boolean i4=str4.equalsIgnoreCase(str5);

System.out.println("比较选定的 String到另一个 String:"+ i4);//如果相同(不区分大小写以及地址),就输出true,否则输出false//format 使用指定的格式字符串和参数返回格式化的字符串。

String str7 ="Hi,%s,%d,%c,%s";

String i6=String.format(str7, "这个人",666,'M',"666666");

System.out.println("使用指定的格式字符串和参数返回格式化的字符串:"+i6);//getBytes 这 String编码成一个序列使用指定的字符的字节,并将结果存储到一个新的字节数组。

byte []i8=str1.getBytes();

System.out.println("一个新的数组:"+Arrays.toString(i8));//indexOf 返回在指定字符的第一个发生的字符串中的索引。

inti7=str1.indexOf('3');

System.out.println("返回在指定字符的第一个发生的字符串中的索引:"+i7);//isEmpty 返回 true如果,如果, length()是 0。

Stringstr8="";boolean i9=str8.isEmpty();

System.out.println("如果选择的参数字符串个数为0,那么输出的为true,否则为flase:"+i9);//lastIndexOf返回在指定字符的最后一个发生的字符串内的索引。

inti10=str1.lastIndexOf('3');

System.out.println("指定的字符最后一个发生牵引的数:"+i10);//length返回此字符串的长度。

int i11=str1.length();

System.out.println("放回字符串的长度:"+i11);//replace 每个子串替换该字符串指定的文本替换序列靶序列匹配的文字。

Stringstr10="abcdew";

Stringi12=str10.replace("a","你好");

System.out.println("替换字符串:"+i12);

Stringi13=str10.replace("abcdew","fdewqt");

System.out.println("替换文本:"+i13);//replaceAll每个子串替换该字符串的给予更换,给 regular expression比赛。

String str11="你是,你是谁,你是哪个";

Stringi14=str11.replaceAll("你是","你好");

System.out.println("替换字符串:"+i14);//split 将此字符串在给定的 regular expression比赛。

Stringi15=str11.replaceAll("你是","12");

System.out.println("替换字符串:"+i15);//startsWith 测试这个字符串是否以指定的前缀开始

Stringstr12="%12sdas%2131";boolean i16=str12.startsWith("%");

System.out.println("测试这个字符串是否以指定的前缀开始,是输出的为true,否则为flase:"+i16);//substring 返回一个字符串,这个字符串的子串。

Stringi17=str12.substring(3);

System.out.println("返回一个字符串,无第3个以前的个字符:"+i17);//trim 返回一个字符串,它的值是字符串,任何前导和尾随空格删除。

Stringstr13=" 12 %12sd as% 2131 ";

Stringi18=str13.trim();

System.out.println("任何前尾随空格都会被删除:"+i18);//valueOf 返回的 boolean参数的字符串表示形式。

String i19=String.valueOf(23);

System.out.println("转换数值类型:"+i19);

}

}2、题目:

定义一个学生的类,

学生的属性:姓名,年龄,性别,身高,体重

定义一个体育项目类

体育项目类包含,打篮球和举重2个方法

学校要求

身高超过170cm的学生要打篮球,

所有学生中体重最重的人要举重

再定义一个主类,主类中包含身高比较的方法,体重比较的方法。

创建三个学生来测试。packagecom.Dcs;public classStudent {privateString name;private intage;private intheight;privateString sex;private intweight;

publicStudent(String name,int age, int height, String sex, intweight) {this.name =name;this.age =age;this.height =height;this.sex =sex;this.weight =weight;

}public Student(intweight) {this.weight =weight;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}public intgetHeight() {returnheight;

}public void setHeight(intheight) {this.height =height;

}publicString getSex() {returnsex;

}public voidsetSex(String sex) {this.sex =sex;

}public intgetWeight() {returnweight;

}public void setWeight(intweight) {this.weight =weight;

}

@OverridepublicString toString() {return "学生{" +

"姓名='" + name + '\'' +

", 年龄=" + age +

", 身高=" + height +

", 性别='" + sex + '\'' +

", 体重='" + weight + '\'' +

'}';

}

}packagecom.Dcs;public classSports {public static voidmain(String[] args) {//学生1

Student xuesheng1 = Sports.createStudent("小宝", 17, 192, "男", 123);

System.out.println(xuesheng1.toString());//学生2

Student xuesheng2 = Sports.createStudent("小洁", 18, 160, "女", 124);

System.out.println(xuesheng2.toString());//学生3

Student xuesheng3 = Sports.createStudent("小熊", 18, 185, "男", 167);

System.out.println(xuesheng3.toString());//方法二输出体重最重的学生//输出体重最重的学生

Sports sports =newSports();

Studentxuesheng=sports.compareStudentToWeight(xuesheng1,xuesheng2,xuesheng3);

System.out.println("举重的学生:"+xuesheng.getName());

sports.jijdai(xuesheng1,xuesheng2,xuesheng3);int height1 =xuesheng1.getHeight();int height2 =xuesheng2.getHeight();int height3 =xuesheng3.getHeight();if (height1 >= 170) {

System.out.println("打篮球的学生:"+xuesheng1.getName());

}if (height2 >= 170) {

System.out.println("打篮球的学生:"+xuesheng2.getName());

}if (height3 >= 170) {

System.out.println("打篮球的学生:"+xuesheng3.getName());

}//输出身高高于170学生

Sports sports1 =newSports();

sports1.compareStudentToHeight(xuesheng1,xuesheng2,xuesheng3);

}//身高高于170打篮球

public voidcompareStudentToHeight(Student xuesheng1, Student xuesheng2, Studentxuesheng3) {int height1 =xuesheng1.getHeight();int height2 =xuesheng2.getHeight();int height3 =xuesheng3.getHeight();if (height1 >= 170) {

System.out.println("打篮球的学生:"+xuesheng1.getName());

}if (height2 >= 170) {

System.out.println("打篮球的学生:"+xuesheng2.getName());

}if (height3 >= 170) {

System.out.println("打篮球的学生:"+xuesheng3.getName());

}

}public static Student createStudent(String name, int age, int height,String sex, intweight) {

Student xuesheng= newStudent(name, age, height, sex, weight);returnxuesheng;

}//对比体重,体重最重的参加举重

publicStudentcompareStudentToWeight(Student xuesheng1, Student xuesheng2, Student xuesheng3){int weight1 =xuesheng1.getWeight();int weight2 =xuesheng2.getWeight();int weight3 =xuesheng3.getWeight();//第一个跟第二个比较

int weight =Math.max(weight1, weight2);

Student xuesheng= weight == weight1 ?xuesheng1 : xuesheng2;//第一个比较完成后,跟第二个比较

int weightj =Math.max(weight, weight3);

Student xueshengj= weightj == weight ?xuesheng : xuesheng3;returnxueshengj;

}//第二种方法比较那个学生的体重

public voidjijdai(Studentxuesheng1, Student xuesheng2, Student xuesheng3) {int weight1 =xuesheng1.getWeight();int weight2 =xuesheng2.getWeight();int weight3 =xuesheng3.getWeight();if (weight1 > weight2 && weight1 >weight3) {

System.out.println("tizhong:"+xuesheng1.getWeight());

}else if (weight2 > weight1 && weight2 >weight3) {

System.out.println("tizhong:"+xuesheng2.getName());

}else{

System.out.println("tizhong:"+xuesheng3.getName());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值