日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

面向对象综合训练综合练习

發布時間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 面向对象综合训练综合练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

面向對象綜合訓練綜合練習

練習一:文字版格斗游戲

需求:

? 格斗游戲,每個游戲角色的姓名,血量,都不相同,在選定人物的時候(new對象的時候),這些信息就應該被確定下來。

舉例:

? 程序運行之后結果為:

? 姓名為:喬峰 血量為:100

? 姓名為:鳩摩智 血量為:100

? 喬峰舉起拳頭打了鳩摩智一下,造成了XX點傷害,鳩摩智還剩下XXX點血。

? 鳩摩智舉起拳頭打了鳩摩智一下,造成了XX點傷害,喬峰還剩下XXX點血。

? 喬峰舉起拳頭打了鳩摩智一下,造成了XX點傷害,鳩摩智還剩下XXX點血。

? 鳩摩智舉起拳頭打了鳩摩智一下,造成了XX點傷害,喬峰還剩下XXX點血。

? 喬峰K.O.了鳩摩智

代碼示例:

public class GameTest {public static void main(String[] args) {//1.創建第一個角色Role r1 = new Role("喬峰",100);//2.創建第二個角色Role r2 = new Role("鳩摩智",100);//3.開始格斗 回合制游戲while(true){//r1開始攻擊r2r1.attack(r2);//判斷r2的剩余血量if(r2.getBlood() == 0){System.out.println(r1.getName() + " K.O了" + r2.getName());break;}//r2開始攻擊r1r2.attack(r1);if(r1.getBlood() == 0){System.out.println(r2.getName() + " K.O了" + r1.getName());break;}}} }public class Role {private String name;private int blood;public Role() {}public Role(String name, int blood) {this.name = name;this.blood = blood;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getBlood() {return blood;}public void setBlood(int blood) {this.blood = blood;}//定義一個方法用于攻擊別人//思考:誰攻擊誰?//Role r1 = new Role();//Role r2 = new Role();//r1.攻擊(r2);//方法的調用者去攻擊參數public void attack(Role role) {//計算造成的傷害 1 ~ 20Random r = new Random();int hurt = r.nextInt(20) + 1;//剩余血量int remainBoold = role.getBlood() - hurt;//對剩余血量做一個驗證,如果為負數了,就修改為0remainBoold = remainBoold < 0 ? 0 : remainBoold;//修改一下挨揍的人的血量role.setBlood(remainBoold);//this表示方法的調用者System.out.println(this.getName() + "舉起拳頭,打了" + role.getName() + "一下," +"造成了" + hurt + "點傷害," + role.getName() + "還剩下了" + remainBoold + "點血");}}

練習二:文字版格斗游戲進階

? 在上一個的基礎上,我想看到人物的性別和長相,打斗的時候我想看到武功招式。

舉例:

? 程序運行之后結果為:

? 姓名為:喬峰 血量為:100 性別為:男 長相為:氣宇軒昂

? 姓名為:鳩摩智 血量為:100 性別為:男 長相為:氣宇軒昂

? 喬峰使出了一招【背心釘】,轉到對方的身后,一掌向鳩摩智背心的靈臺穴拍去。給鳩摩智造成一處瘀傷。

? 鳩摩智使出了一招【游空探爪】,飛起身形自半空中變掌為抓鎖向喬峰。結果喬峰退了半步,毫發無損。

? 。。。。

? 喬峰K.O.了鳩摩智

分析:

? 長相是提前定義好的,提前放在一個數組當中,程序運行之后,從數組中隨機獲取。

//男生長相數組 String[] boyfaces = {"風流俊雅", "氣宇軒昂", "相貌英俊", "五官端正", "相貌平平", "一塌糊涂", "面目猙獰"}; //女生長相數組 String[] girlfaces = {"美奐絕倫", "沉魚落雁", "婷婷玉立", "身材嬌好", "相貌平平", "相貌簡陋", "慘不忍睹"};

? 武功招式也是提前定義好的,提前放在一個數組當中,程序運行之后,從數組隨機獲取

//attack 攻擊描述: String[] attacks_desc = {"%s使出了一招【背心釘】,轉到對方的身后,一掌向%s背心的靈臺穴拍去。","%s使出了一招【游空探爪】,飛起身形自半空中變掌為抓鎖向%s。","%s大喝一聲,身形下伏,一招【劈雷墜地】,捶向%s雙腿。","%s運氣于掌,一瞬間掌心變得血紅,一式【掌心雷】,推向%s。","%s陰手翻起陽手跟進,一招【沒遮攔】,結結實實的捶向%s。","%s上步搶身,招中套招,一招【劈掛連環】,連環攻向%s。"

? 受傷的提前也是提前定義好的,只不過不是隨機了,根據剩余血量獲取不同的描述

//injured 受傷描述: String[] injureds_desc = {"結果%s退了半步,毫發無損","結果給%s造成一處瘀傷","結果一擊命中,%s痛得彎下腰","結果%s痛苦地悶哼了一聲,顯然受了點內傷","結果%s搖搖晃晃,一跤摔倒在地","結果%s臉色一下變得慘白,連退了好幾步","結果『轟』的一聲,%s口中鮮血狂噴而出","結果%s一聲慘叫,像灘軟泥般塌了下去"

? 其中輸出語句跟以前不一樣了,用的是System.out.printf();該輸出語句支持%s占位符

public class Test {public static void main(String[] args) {//兩部分參數://第一部分參數:要輸出的內容%s(占位)//第二部分參數:填充的數據System.out.printf("你好啊%s","張三");//用張三填充第一個%sSystem.out.println();//換行System.out.printf("%s你好啊%s","張三","李四");//用張三填充第一個%s,李四填充第二個%s} }

最終代碼示例:

package com.itheima.test2;import java.util.Random;public class Role {private String name;private int blood;private char gender;private String face;//長相是隨機的String[] boyfaces = {"風流俊雅", "氣宇軒昂", "相貌英俊", "五官端正", "相貌平平", "一塌糊涂", "面目猙獰"};String[] girlfaces = {"美奐絕倫", "沉魚落雁", "婷婷玉立", "身材嬌好", "相貌平平", "相貌簡陋", "慘不忍睹"};//attack 攻擊描述:String[] attacks_desc = {"%s使出了一招【背心釘】,轉到對方的身后,一掌向%s背心的靈臺穴拍去。","%s使出了一招【游空探爪】,飛起身形自半空中變掌為抓鎖向%s。","%s大喝一聲,身形下伏,一招【劈雷墜地】,捶向%s雙腿。","%s運氣于掌,一瞬間掌心變得血紅,一式【掌心雷】,推向%s。","%s陰手翻起陽手跟進,一招【沒遮攔】,結結實實的捶向%s。","%s上步搶身,招中套招,一招【劈掛連環】,連環攻向%s。"};//injured 受傷描述:String[] injureds_desc = {"結果%s退了半步,毫發無損","結果給%s造成一處瘀傷","結果一擊命中,%s痛得彎下腰","結果%s痛苦地悶哼了一聲,顯然受了點內傷","結果%s搖搖晃晃,一跤摔倒在地","結果%s臉色一下變得慘白,連退了好幾步","結果『轟』的一聲,%s口中鮮血狂噴而出","結果%s一聲慘叫,像灘軟泥般塌了下去"};public Role() {}public Role(String name, int blood, char gender) {this.name = name;this.blood = blood;this.gender = gender;//隨機長相setFace(gender);}public char getGender() {return gender;}public void setGender(char gender) {this.gender = gender;}public String getFace() {return face;}public void setFace(char gender) {Random r = new Random();//長相是隨機的if (gender == '男') {//從boyfaces里面隨機長相int index = r.nextInt(boyfaces.length);this.face = boyfaces[index];} else if (gender == '女') {//從girlfaces里面隨機長相int index = r.nextInt(girlfaces.length);this.face = girlfaces[index];} else {this.face = "面目猙獰";}}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getBlood() {return blood;}public void setBlood(int blood) {this.blood = blood;}//定義一個方法用于攻擊別人//思考:誰攻擊誰?//Role r1 = new Role();//Role r2 = new Role();//r1.攻擊(r2);//方法的調用者去攻擊參數public void attack(Role role) {Random r = new Random();int index = r.nextInt(attacks_desc.length);String KungFu = attacks_desc[index];//輸出一個攻擊的效果System.out.printf(KungFu, this.getName(), role.getName());System.out.println();//計算造成的傷害 1 ~ 20int hurt = r.nextInt(20) + 1;//剩余血量int remainBoold = role.getBlood() - hurt;//對剩余血量做一個驗證,如果為負數了,就修改為0remainBoold = remainBoold < 0 ? 0 : remainBoold;//修改一下挨揍的人的血量role.setBlood(remainBoold);//受傷的描述//血量> 90 0索引的描述//80 ~ 90 1索引的描述//70 ~ 80 2索引的描述//60 ~ 70 3索引的描述//40 ~ 60 4索引的描述//20 ~ 40 5索引的描述//10 ~ 20 6索引的描述//小于10的 7索引的描述if (remainBoold > 90) {System.out.printf(injureds_desc[0], role.getName());}else if(remainBoold > 80 && remainBoold <= 90){System.out.printf(injureds_desc[1], role.getName());}else if(remainBoold > 70 && remainBoold <= 80){System.out.printf(injureds_desc[2], role.getName());}else if(remainBoold > 60 && remainBoold <= 70){System.out.printf(injureds_desc[3], role.getName());}else if(remainBoold > 40 && remainBoold <= 60){System.out.printf(injureds_desc[4], role.getName());}else if(remainBoold > 20 && remainBoold <= 40){System.out.printf(injureds_desc[5], role.getName());}else if(remainBoold > 10 && remainBoold <= 20){System.out.printf(injureds_desc[6], role.getName());}else{System.out.printf(injureds_desc[7], role.getName());}System.out.println();}public void showRoleInfo() {System.out.println("姓名為:" + getName());System.out.println("血量為:" + getBlood());System.out.println("性別為:" + getGender());System.out.println("長相為:" + getFace());}}package com.itheima.test2;public class GameTest {public static void main(String[] args) {//1.創建第一個角色Role r1 = new Role("喬峰",100,'男');//2.創建第二個角色Role r2 = new Role("鳩摩智",100,'男');//展示一下角色的信息r1.showRoleInfo();r2.showRoleInfo();//3.開始格斗 回合制游戲while(true){//r1開始攻擊r2r1.attack(r2);//判斷r2的剩余血量if(r2.getBlood() == 0){System.out.println(r1.getName() + " K.O了" + r2.getName());break;}//r2開始攻擊r1r2.attack(r1);if(r1.getBlood() == 0){System.out.println(r2.getName() + " K.O了" + r1.getName());break;}}} }

練習三:對象數組(商品)

需求:

? 定義數組存儲3個商品對象。

? 商品的屬性:商品的id,名字,價格,庫存。

? 創建三個商品對象,并把商品對象存入到數組當中。

代碼示例:

package com.itheima.test3;public class Goods {private String id;private String name;private double price;private int count;public Goods() {}public Goods(String id, String name, double price, int count) {this.id = id;this.name = name;this.price = price;this.count = count;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getCount() {return count;}public void setCount(int count) {this.count = count;} }package com.itheima.test3;public class GoodsTest {public static void main(String[] args) {//1.創建一個數組Goods[] arr = new Goods[3];//2.創建三個商品對象Goods g1 = new Goods("001","華為P40",5999.0,100);Goods g2 = new Goods("002","保溫杯",227.0,50);Goods g3 = new Goods("003","枸杞",12.7,70);//3.把商品添加到數組中arr[0] = g1;arr[1] = g2;arr[2] = g3;//4.遍歷for (int i = 0; i < arr.length; i++) {//i 索引 arr[i] 元素Goods goods = arr[i];System.out.println(goods.getId() + ", " + goods.getName() + ", " + goods.getPrice() + ", " + goods.getCount());}} }

練習四:對象數組(汽車)

需求:

? 定義數組存儲3部汽車對象。

? 汽車的屬性:品牌,價格,顏色。

? 創建三個汽車對象,數據通過鍵盤錄入而來,并把數據存入到數組當中。

代碼示例:

package com.itheima.test5;public class Car {private String brand;//品牌private int price;//價格private String color;//顏色public Car() {}public Car(String brand, int price, String color) {this.brand = brand;this.price = price;this.color = color;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public String getColor() {return color;}public void setColor(String color) {this.color = color;} }package com.itheima.test5;import java.util.Scanner;public class CarTest {public static void main(String[] args) {//1.創建一個數組用來存3個汽車對象Car[] arr = new Car[3];//2.創建汽車對象,數據來自于鍵盤錄入Scanner sc = new Scanner(System.in);for (int i = 0; i < arr.length; i++) {//創建汽車的對象Car c = new Car();//錄入品牌System.out.println("請輸入汽車的品牌");String brand = sc.next();c.setBrand(brand);//錄入價格System.out.println("請輸入汽車的價格");int price = sc.nextInt();c.setPrice(price);//錄入顏色System.out.println("請輸入汽車的顏色");String color = sc.next();c.setColor(color);//把汽車對象添加到數組當中arr[i] = c;}//3.遍歷數組for (int i = 0; i < arr.length; i++) {Car car = arr[i];System.out.println(car.getBrand() + ", " + car.getPrice() + ", " + car.getColor());}} }

練習五:對象數組(手機)

需求 :

? 定義數組存儲3部手機對象。

? 手機的屬性:品牌,價格,顏色。

? 要求,計算出三部手機的平均價格

代碼示例:

package com.itheima.test6;public class Phone {private String brand;//品牌private int price;//價格private String color;//顏色public Phone() {}public Phone(String brand, int price, String color) {this.brand = brand;this.price = price;this.color = color;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public String getColor() {return color;}public void setColor(String color) {this.color = color;} }package com.itheima.test6;import java.math.BigDecimal;public class PhoneTest {public static void main(String[] args) {//1.創建一個數組Phone[] arr = new Phone[3];//2.創建手機的對象Phone p1 = new Phone("小米",1999,"白色");Phone p2 = new Phone("華為",4999,"藍色");Phone p3 = new Phone("魅族",3999,"紅色");//3.把手機對象添加到數組當中arr[0] = p1;arr[1] = p2;arr[2] = p3;//4.獲取三部手機的平均價格int sum = 0;for (int i = 0; i < arr.length; i++) {//i 索引 arr[i] 元素(手機對象)Phone phone = arr[i];sum = sum + phone.getPrice();}//5.求平均值//數據能不寫死,盡量不寫死//int avg = sum / arr.length;double avg2 = sum * 1.0 / arr.length;System.out.println(avg2);//3665.6666666666665} }

練習六:對象數組(女朋友)

需求:

? 定義數組存儲4個女朋友的對象

? 女朋友的屬性:姓名、年齡、性別、愛好

? 要求1:計算出四女朋友的平均年齡

? 要求2:統計年齡比平均值低的女朋友有幾個?并把她們的所有信息打印出來。

代碼示例:

package com.itheima.test7;public class GirlFriend {private String name;//姓名private int age;//年齡private String gender;//性別private String hobby;//愛好public GirlFriend() {}public GirlFriend(String name, int age, String gender, String hobby) {this.name = name;this.age = age;this.gender = gender;this.hobby = hobby;}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 getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;} }package com.itheima.test7;public class GirlFriendTest {public static void main(String[] args) {//1.定義數組存入女朋友的對象GirlFriend[] arr = new GirlFriend[4];//2.創建女朋友對象GirlFriend gf1 = new GirlFriend("小詩詩",18,"萌妹子","吃零食");GirlFriend gf2 = new GirlFriend("小丹丹",19,"萌妹子","玩游戲");GirlFriend gf3 = new GirlFriend("小惠惠",20,"萌妹子","看書,學習");GirlFriend gf4 = new GirlFriend("小莉莉",21,"憨妹子","睡覺");//3.把對象添加到數組當中arr[0] = gf1;arr[1] = gf2;arr[2] = gf3;arr[3] = gf4;//4.求和int sum = 0;for (int i = 0; i < arr.length; i++) {//i 索引 arr[i] 元素(女朋友對象)GirlFriend gf = arr[i];//累加sum = sum + gf.getAge();}//5.平均值int avg = sum / arr.length;//6.統計年齡比平均值低的有幾個,打印他們的信息int count = 0;for (int i = 0; i < arr.length; i++) {GirlFriend gf = arr[i];if(gf.getAge() < avg){count++;System.out.println(gf.getName() + ", " + gf.getAge() + ", " + gf.getGender() + ", " + gf.getHobby());}}System.out.println(count + "個");} }

練習七:復雜的對象數組操作

定義一個長度為3的數組,數組存儲1~3名學生對象作為初始數據,學生對象的學號,姓名各不相同。

學生的屬性:學號,姓名,年齡。

要求1:再次添加一個學生對象,并在添加的時候進行學號的唯一性判斷。

要求2:添加完畢之后,遍歷所有學生信息。

要求3:通過id刪除學生信息

? 如果存在,則刪除,如果不存在,則提示刪除失敗。

要求4:刪除完畢之后,遍歷所有學生信息。

要求5:查詢數組id為“heima002”的學生,如果存在,則將他的年齡+1歲

代碼示例:

package com.itheima.test8;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 class Test {public static void main(String[] args) {/*定義一個長度為3的數組,數組存儲1~3名學生對象作為初始數據,學生對象的學號,姓名各不相同。學生的屬性:學號,姓名,年齡。要求1:再次添加一個學生對象,并在添加的時候進行學號的唯一性判斷。要求2:添加完畢之后,遍歷所有學生信息。*///1.創建一個數組用來存儲學生對象Student[] arr = new Student[3];//2.創建學生對象并添加到數組當中Student stu1 = new Student(1, "zhangsan", 23);Student stu2 = new Student(2, "lisi", 24);//3.把學生對象添加到數組當中arr[0] = stu1;arr[1] = stu2;//要求1:再次添加一個學生對象,并在添加的時候進行學號的唯一性判斷。Student stu4 = new Student(1, "zhaoliu", 26);//唯一性判斷//已存在 --- 不用添加//不存在 --- 就可以把學生對象添加進數組boolean flag = contains(arr, stu4.getId());if(flag){//已存在 --- 不用添加System.out.println("當前id重復,請修改id后再進行添加");}else{//不存在 --- 就可以把學生對象添加進數組//把stu4添加到數組當中//1.數組已經存滿 --- 只能創建一個新的數組,新數組的長度 = 老數組 + 1//2.數組沒有存滿 --- 直接添加int count = getCount(arr);if(count == arr.length){//已經存滿//創建一個新的數組,長度 = 老數組的長度 + 1//然后把老數組的元素,拷貝到新數組當中Student[] newArr = creatNewArr(arr);//把stu4添加進去newArr[count] = stu4;//要求2:添加完畢之后,遍歷所有學生信息。printArr(newArr);}else{//沒有存滿//[stu1,stu2,null]//getCount獲取到的是2,表示數組當中已經有了2個元素//還有一層意思:如果下一次要添加數據,就是添加到2索引的位置arr[count] = stu4;//要求2:添加完畢之后,遍歷所有學生信息。printArr(arr);}}}public static void printArr(Student[] arr){for (int i = 0; i < arr.length; i++) {Student stu = arr[i];if(stu != null){System.out.println(stu.getId() + ", " + stu.getName() + ", " + stu.getAge());}}}//創建一個新的數組,長度 = 老數組的長度 + 1//然后把老數組的元素,拷貝到新數組當中public static Student[] creatNewArr(Student[] arr){Student[] newArr = new Student[arr.length + 1];//循環遍歷得到老數組中的每一個元素for (int i = 0; i < arr.length; i++) {//把老數組中的元素添加到新數組當中newArr[i] = arr[i];}//把新數組返回return newArr;}//定義一個方法判斷數組中已經存了幾個元素public static int getCount(Student[] arr){//定義一個計數器用來統計int count = 0;for (int i = 0; i < arr.length; i++) {if(arr[i] != null){count++;}}//當循環結束之后,我就知道了數組中一共有幾個元素return count;}//1.我要干嘛? 唯一性判斷//2.我干這件事情,需要什么才能完成? 數組 id//3.調用處是否需要繼續使用方法的結果? 必須返回public static boolean contains(Student[] arr, int id) {for (int i = 0; i < arr.length; i++) {//依次獲取到數組里面的每一個學生對象Student stu = arr[i];if(stu != null){//獲取數組中學生對象的idint sid = stu.getId();//比較if(sid == id){return true;}}}//當循環結束之后,還沒有找到一樣的,那么就表示數組中要查找的id是不存在的。return false;}} package com.itheima.test8;public class Test3 {public static void main(String[] args) {/*定義一個長度為3的數組,數組存儲1~3名學生對象作為初始數據,學生對象的學號,姓名各不相同。學生的屬性:學號,姓名,年齡。要求3:通過id刪除學生信息如果存在,則刪除,如果不存在,則提示刪除失敗。要求4:刪除完畢之后,遍歷所有學生信息。*///1.創建一個數組用來存儲學生對象Student[] arr = new Student[3];//2.創建學生對象并添加到數組當中Student stu1 = new Student(1, "zhangsan", 23);Student stu2 = new Student(2, "lisi", 24);Student stu3 = new Student(3, "wangwu", 25);//3.把學生對象添加到數組當中arr[0] = stu1;arr[1] = stu2;arr[2] = stu3;/*要求3:通過id刪除學生信息如果存在,則刪除,如果不存在,則提示刪除失敗。*///要找到id在數組中對應的索引int index = getIndex(arr, 2);if (index >= 0){//如果存在,則刪除arr[index] = null;//遍歷數組printArr(arr);}else{//如果不存在,則提示刪除失敗System.out.println("當前id不存在,刪除失敗");}}//1.我要干嘛? 找到id在數組中的索引//2.我需要什么? 數組 id//3.調用處是否需要繼續使用方法的結果? 要public static int getIndex(Student[] arr , int id){for (int i = 0; i < arr.length; i++) {//依次得到每一個學生對象Student stu = arr[i];//對stu進行一個非空判斷if(stu != null){int sid = stu.getId();if(sid == id){return i;}}}//當循環結束之后,還沒有找到就表示不存在return -1;}public static void printArr(Student[] arr){for (int i = 0; i < arr.length; i++) {Student stu = arr[i];if(stu != null){System.out.println(stu.getId() + ", " + stu.getName() + ", " + stu.getAge());}}}} package com.itheima.test8;public class Test4 {public static void main(String[] args) {/*定義一個長度為3的數組,數組存儲1~3名學生對象作為初始數據,學生對象的學號,姓名各不相同。學生的屬性:學號,姓名,年齡。要求5:查詢數組id為“2”的學生,如果存在,則將他的年齡+1歲*///1.創建一個數組用來存儲學生對象Student[] arr = new Student[3];//2.創建學生對象并添加到數組當中Student stu1 = new Student(1, "zhangsan", 23);Student stu2 = new Student(2, "lisi", 24);Student stu3 = new Student(3, "wangwu", 25);//3.把學生對象添加到數組當中arr[0] = stu1;arr[1] = stu2;arr[2] = stu3;//4.先要找到id為2的學生對于的索引int index = getIndex(arr, 2);//5.判斷索引if(index >= 0){//存在, 則將他的年齡+1歲Student stu = arr[index];//把原來的年齡拿出來int newAge = stu.getAge() + 1;//把+1之后的年齡塞回去stu.setAge(newAge);//遍歷數組printArr(arr);}else{//不存在,則直接提示System.out.println("當前id不存在,修改失敗");}}//1.我要干嘛? 找到id在數組中的索引//2.我需要什么? 數組 id//3.調用處是否需要繼續使用方法的結果? 要public static int getIndex(Student[] arr , int id){for (int i = 0; i < arr.length; i++) {//依次得到每一個學生對象Student stu = arr[i];//對stu進行一個非空判斷if(stu != null){int sid = stu.getId();if(sid == id){return i;}}}//當循環結束之后,還沒有找到就表示不存在return -1;}public static void printArr(Student[] arr){for (int i = 0; i < arr.length; i++) {Student stu = arr[i];if(stu != null){System.out.println(stu.getId() + ", " + stu.getName() + ", " + stu.getAge());}}} }

總結

以上是生活随笔為你收集整理的面向对象综合训练综合练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。