[Java学习笔记]面向对象综合训练

编程入门 行业动态 更新时间:2024-10-06 18:34:51

[Java学习笔记]<a href=https://www.elefans.com/category/jswz/34/1769334.html style=面向对象综合训练"/>

[Java学习笔记]面向对象综合训练

1.Scanner类的方法中,next()与nextLine()有什么区别?在使用的时候需要注意什么?

next()遇到空格、制表符、回车就停止,当然nextInt、Double()也是如此。
nextline()只会在按回车的时候停止。

使用请注意:

scanner.nextLine()最好不要和其他键盘录入混用:比如nextLine()会存储着next()未能接收的字符

下面是错误使用案例:

public class Test {public static void main(String[] args) {Scanner scanner=new Scanner(System.in);//输入1:第一段字符串 第二段字符串,前面还有一个空格//输入2:第一段字符串,第二段是一个回车符 回车String str1=scanner.next();System.out.println(str1);String str2=scanner.nextLine();System.out.println(str2);}
}

输出结果一: 

输出结果二: 


 2.文字版格斗游戏代码

测试类:

public class GameTest {public static void main(String[] args) {//初始化变量Role role1 = new Role("小军", 100,'女');Role role2 = new Role("小红", 100,'男');//展示角色信息role1.showRoleInfo();role2.showRoleInfo();//进行战斗while (true) {role1.attack(role2);if (role2.getHP() == 0) {System.out.println(role1.getName() + "击败了" + role2.getName());break;}role2.attack(role1);if (role1.getHP() == 0) {System.out.println(role2.getName() + "击败了" + role1.getName());break;}}}
}

JavaBean类:

//该练习需要掌握的地方:
// 学会在数组中存储数据,并且存放%s用于printf的打印
//在构造方法中使用Set变量方法构造随机的成员变量
//在javaBean的方法中尽量使用.get与.set方法获取和改变成员变量//角色类:存放变量,构造方法,GetSet方法,展示信息方法,战斗方法
public class Role {private String name;private int HP;private char gender;private String face;String[] boyFaces = {"风流俊雅","气宇轩昂","相貌英俊","五官端正","相貌平平","一塌糊涂","面目狰狞"};String[] girlFaces ={"美奂绝伦","沉鱼落雁","婷婷玉立","身材娇好","相貌平平","相貌简陋","惨不忍睹"};String[] attacks_desc={"%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去。","%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s。","%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿。","%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s。","%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s。","%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s。"};String[] injured_desc={"结果%s退了半步,毫发无损","结果给%s造成一处瘀伤","结果一击命中,%s痛得弯下腰","结果%s痛苦地闷哼了一声,显然受了点内伤","结果%s摇摇晃晃,一跤摔倒在地","结果%s脸色一下变得惨白,连退了好几步","结果『轰』的一声,%s口中鲜血狂喷而出","结果%s一声惨叫,像滩软泥般塌了下去"};//构造方法public Role() {}public Role(String name, int HP,char gender) {this.setName(name);this.setHP(HP);this.setGender(gender);this.setFace();}public String getName() { return name; }public void setName(String name) {this.name = name;}public int getHP() {return HP;}public void setHP(int HP) {this.HP = HP;}public char getGender() { return gender; }public void setGender(char gender) {this.gender = gender; }public String getFace() {return face;}public void setFace() {Random random=new Random();if(this.gender=='男'){int index=random.nextInt(boyFaces.length);this.face= boyFaces[index];}else {int index=random.nextInt(girlFaces.length);this.face= girlFaces[index];}}public void  showRoleInfo(){System.out.println(this.getName()+" "+this.getGender()+" "+this.getFace());}public void attack(Role role) {//如果我写的话会写两个对象参数,忘记了用this.可以获取使用者方法者的地址,这里学习一下黑马的写法Random random = new Random();//打斗数值描述:取随机伤害,并且限定数值范围int hurt = random.nextInt(20) + 1;int remainBlood = role.getHP() - hurt;remainBlood = remainBlood < 0 ? 0 : remainBlood;role.setHP(remainBlood);System.out.println(this.getName() + "攻击力" + role.getName() + "造成了" + hurt + "点伤害。"+role.getName() + "剩余血量为" + role.getHP());//打斗详情描述:攻击用随机招数,受伤根据血量判断int  attack=random.nextInt(attacks_desc.length);System.out.printf(attacks_desc[attack],this.getName(),role.getName());if(remainBlood>=90){System.out.printf(injured_desc[0],role.getName());}else if(remainBlood>=80){System.out.printf(injured_desc[1],role.getName());}else if(remainBlood>=70){System.out.printf(injured_desc[2],role.getName());}else if(remainBlood>=60){System.out.printf(injured_desc[3],role.getName());}else if(remainBlood>=40){System.out.printf(injured_desc[4],role.getName());}else if(remainBlood>=20){System.out.printf(injured_desc[5],role.getName());}else if(remainBlood>=10){System.out.printf(injured_desc[6],role.getName());} else{System.out.printf(injured_desc[7],role.getName());}System.out.println();}
}

测试结果:


3. 定义数组存储两部汽车对象,创建两个汽车对象,通过键盘录入把数据存储到数组当中

测试类:

public class CarTest {public static void main(String[] args) {Scanner scanner=new Scanner(System.in);Car[] cars=new Car[2];//在用数组声明对象的时候并没有在内存中创建多个对象,只是声明了要存储的对象类型与个数System.out.println(cars);System.out.println(cars[0]);for (int i = 0; i < cars.length; i++) {Car car=new Car();//车对象不能写在循环外边System.out.println("请输入价格:");int price=scanner.nextInt();car.setPrice(price);System.out.println("请输入品牌:");String brand=scanner.next();car.setBrand(brand);System.out.println("请输入颜色:");String color=scanner.next();car.setColor(color);cars[i]=car;}for (int i = 0; i < cars.length; i++) {System.out.println(cars[i]);}}
}

JavaBean类:

public class Car {private  int price;private  String brand;private  String color;public Car() {}public Car(int price, String brand, String color) {this.price = price;this.brand = brand;this.color = color;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String toString() {return "Car{price = " + price + ", brand = " + brand + ", color = " + color + "}";}
}

 


 

4.创建长度为三的学生对象数组,并且创建对象放入数组 ,再次创建对象并且放入数组,其中需要判断id是否重复,数组是否已满,最后遍历数组中的所有对象

本练习中有三点需要注意:

问题一:对象数组中遇到空对象不能遍历,并且报错。这里使用了一个非空判断来解决问题,而且下面的静态函数有很多都用到了非空判断。
问题二:数组容量不够的时候,老师这里的练习是要开辟新数组并且复制旧数组到新数组(因为这是新手练习),我觉得这种写法不太好,不如C语言中的链表,这个知识点到后面再学习。
问题三:视频中第四题第五题,老师用的是获取下标再更改数据,但是由于这里的id不是字符串类型,直接用属性id去获取数组中的对象也可以,我没有做这两问,不过这个思路是一定要知道的。

测试类: 

public class StudentTest {//创建长度为三的学生对象数组,并且创建对象放入数组//再次创建对象并且放入数组,其中得判断id是否重复,数组是否已满,最后打印public static void main(String[] args) {Student[] students=new Student[3];Student student1=new Student(1,"zimo",20);Student student2=new Student(2,"baiyi",20);Student student3=new Student(3,"yutian",20);students[0]=student1;students[1]=student2;students[2]=student3;Student student4=new Student(4,"sale",20);boolean flag = contains(students,student4.getId());int count=getCount(students);if(flag){//id重复System.out.println("当前id重复");}else{//id不重复if(students.length==count){//数组长度到达最大,创建新数组并替换旧数组Student[] newStudents=createNew(students);students=newStudents;students[count]=student4;print(students);}else{//数组长度未达到最大students[count]=student4;print(students);}}}public  static boolean contains(Student[] students,int id){//判断对象数组中是否有对象有某id的for (int i = 0; i < students.length; i++) {if (students[i]!=null){if(students[i].getId()==id){return true;}}}return false;}public static int getCount(Student[] students){//计算对象数组中的对象个数int count=0;for (int i = 0; i < students.length; i++){if(students[i]!=null){count++;}}return count;}public static Student[] createNew(Student[] students){//创建比原先对象数组多一个位置的新对象数组并且赋值Student[] arr=new Student[students.length+1];for (int i = 0; i < students.length; i++) {arr[i]=students[i];}return arr;}public  static void print(Student[] students){//打印对象数组for (int i = 0; i < students.length; i++) {if (students[i]!=null){System.out.println(students[i]);}}}
}

JavaBean类:

public class Student {private int id;private String name;private int age;public Student() {}public Student(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String toString() {return "Student{id = " + id + ", name = " + name + ", age = " + age + "}";}
}

更多推荐

[Java学习笔记]面向对象综合训练

本文发布于:2024-02-13 12:06:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1758475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:面向对象   学习笔记   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!