练习一:文字版格斗游戏
需求:
格斗游戏,每个游戏角色的姓名,血量,都不相同,在选定人物的时候(new对象的时候),这些信息就应该被确定下来。
举例:
程序运行之后结果为:
姓名为:乔峰 血量为:100
姓名为:鸠摩智 血量为:100
乔峰举起拳头打了鸠摩智一下,造成了XX点伤害,鸠摩智还剩下XXX点血。
鸠摩智举起拳头打了鸠摩智一下,造成了XX点伤害,乔峰还剩下XXX点血。
乔峰举起拳头打了鸠摩智一下,造成了XX点伤害,鸠摩智还剩下XXX点血。
鸠摩智举起拳头打了鸠摩智一下,造成了XX点伤害,乔峰还剩下XXX点血。
乔峰K.O.了鸠摩智
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| public class GameTest { public static void main(String[] args) { Role r1 = new Role("乔峰",100); Role r2 = new Role("鸠摩智",100);
while(true){ r1.attack(r2); if(r2.getBlood() == 0){ System.out.println(r1.getName() + " K.O了" + r2.getName()); break; }
r2.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; }
public void attack(Role role) { Random r = new Random(); int hurt = r.nextInt(20) + 1;
int remainBoold = role.getBlood() - hurt; remainBoold = remainBoold < 0 ? 0 : remainBoold; role.setBlood(remainBoold);
System.out.println(this.getName() + "举起拳头,打了" + role.getName() + "一下," + "造成了" + hurt + "点伤害," + role.getName() + "还剩下了" + remainBoold + "点血"); }
}
|
练习二:文字版格斗游戏进阶
在上一个的基础上,我想看到人物的性别和长相,打斗的时候我想看到武功招式。
举例:
程序运行之后结果为:
姓名为:乔峰 血量为:100 性别为:男 长相为:气宇轩昂
姓名为:鸠摩智 血量为:100 性别为:男 长相为:气宇轩昂
乔峰使出了一招【背心钉】,转到对方的身后,一掌向鸠摩智背心的灵台穴拍去。给鸠摩智造成一处瘀伤。
鸠摩智使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向乔峰。结果乔峰退了半步,毫发无损。
。。。。
乔峰K.O.了鸠摩智
分析:
长相是提前定义好的,提前放在一个数组当中,程序运行之后,从数组中随机获取。
1 2 3 4
| String[] boyfaces = {"风流俊雅", "气宇轩昂", "相貌英俊", "五官端正", "相貌平平", "一塌糊涂", "面目狰狞"};
String[] girlfaces = {"美奂绝伦", "沉鱼落雁", "婷婷玉立", "身材娇好", "相貌平平", "相貌简陋", "惨不忍睹"};
|
武功招式也是提前定义好的,提前放在一个数组当中,程序运行之后,从数组随机获取
1 2 3 4 5 6 7 8
| String[] attacks_desc = { "%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去。", "%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s。", "%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿。", "%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s。", "%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s。", "%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s。"
|
受伤的提前也是提前定义好的,只不过不是随机了,根据剩余血量获取不同的描述
1 2 3 4 5 6 7 8 9 10
| String[] injureds_desc = { "结果%s退了半步,毫发无损", "结果给%s造成一处瘀伤", "结果一击命中,%s痛得弯下腰", "结果%s痛苦地闷哼了一声,显然受了点内伤", "结果%s摇摇晃晃,一跤摔倒在地", "结果%s脸色一下变得惨白,连退了好几步", "结果『轰』的一声,%s口中鲜血狂喷而出", "结果%s一声惨叫,像滩软泥般塌了下去"
|
其中输出语句跟以前不一样了,用的是System.out.printf();该输出语句支持%s占位符
1 2 3 4 5 6 7 8 9 10 11
| public class Test { public static void main(String[] args) { System.out.printf("你好啊%s","张三"); System.out.println(); System.out.printf("%s你好啊%s","张三","李四"); } }
|
最终代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| 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 = {"美奂绝伦", "沉鱼落雁", "婷婷玉立", "身材娇好", "相貌平平", "相貌简陋", "惨不忍睹"};
String[] attacks_desc = { "%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去。", "%s使出了一招【游空探爪】,飞起身形自半空中变掌为抓锁向%s。", "%s大喝一声,身形下伏,一招【劈雷坠地】,捶向%s双腿。", "%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s。", "%s阴手翻起阳手跟进,一招【没遮拦】,结结实实的捶向%s。", "%s上步抢身,招中套招,一招【劈挂连环】,连环攻向%s。" };
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 == '男') { int index = r.nextInt(boyfaces.length); this.face = boyfaces[index]; } else if (gender == '女') { 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; }
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();
int hurt = r.nextInt(20) + 1;
int remainBoold = role.getBlood() - hurt; remainBoold = remainBoold < 0 ? 0 : remainBoold; role.setBlood(remainBoold);
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) { Role r1 = new Role("乔峰",100,'男'); Role r2 = new Role("鸠摩智",100,'男');
r1.showRoleInfo(); r2.showRoleInfo();
while(true){ r1.attack(r2); if(r2.getBlood() == 0){ System.out.println(r1.getName() + " K.O了" + r2.getName()); break; }
r2.attack(r1); if(r1.getBlood() == 0){ System.out.println(r2.getName() + " K.O了" + r1.getName()); break; } } } }
|
练习三:对象数组(商品)
需求:
定义数组存储3个商品对象。
商品的属性:商品的id,名字,价格,库存。
创建三个商品对象,并把商品对象存入到数组当中。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| 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) { Goods[] arr = new Goods[3];
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);
arr[0] = g1; arr[1] = g2; arr[2] = g3;
for (int i = 0; i < arr.length; i++) { Goods goods = arr[i]; System.out.println(goods.getId() + ", " + goods.getName() + ", " + goods.getPrice() + ", " + goods.getCount()); } } }
|
练习四:对象数组(汽车)
需求:
定义数组存储3部汽车对象。
汽车的属性:品牌,价格,颜色。
创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组当中。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| 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) { Car[] arr = new Car[3];
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; }
for (int i = 0; i < arr.length; i++) { Car car = arr[i]; System.out.println(car.getBrand() + ", " + car.getPrice() + ", " + car.getColor()); } } }
|
练习五:对象数组(手机)
需求 :
定义数组存储3部手机对象。
手机的属性:品牌,价格,颜色。
要求,计算出三部手机的平均价格
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| 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) { Phone[] arr = new Phone[3];
Phone p1 = new Phone("小米",1999,"白色"); Phone p2 = new Phone("华为",4999,"蓝色"); Phone p3 = new Phone("魅族",3999,"红色");
arr[0] = p1; arr[1] = p2; arr[2] = p3;
int sum = 0; for (int i = 0; i < arr.length; i++) { Phone phone = arr[i]; sum = sum + phone.getPrice(); }
double avg2 = sum * 1.0 / arr.length;
System.out.println(avg2); } }
|
练习六:对象数组(女朋友)
需求:
定义数组存储4个女朋友的对象
女朋友的属性:姓名、年龄、性别、爱好
要求1:计算出四女朋友的平均年龄
要求2:统计年龄比平均值低的女朋友有几个?并把她们的所有信息打印出来。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| 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) { GirlFriend[] arr = new GirlFriend[4];
GirlFriend gf1 = new GirlFriend("小诗诗",18,"萌妹子","吃零食"); GirlFriend gf2 = new GirlFriend("小丹丹",19,"萌妹子","玩游戏"); GirlFriend gf3 = new GirlFriend("小惠惠",20,"萌妹子","看书,学习"); GirlFriend gf4 = new GirlFriend("小莉莉",21,"憨妹子","睡觉");
arr[0] = gf1; arr[1] = gf2; arr[2] = gf3; arr[3] = gf4;
int sum = 0; for (int i = 0; i < arr.length; i++) { GirlFriend gf = arr[i]; sum = sum + gf.getAge(); }
int avg = sum / arr.length;
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岁
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| 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; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| public class Test { public static void main(String[] args) {
Student[] arr = new Student[3]; Student stu1 = new Student(1, "zhangsan", 23); Student stu2 = new Student(2, "lisi", 24);
arr[0] = stu1; arr[1] = stu2;
Student stu4 = new Student(1, "zhaoliu", 26);
boolean flag = contains(arr, stu4.getId()); if(flag){ System.out.println("当前id重复,请修改id后再进行添加"); }else{ int count = getCount(arr); if(count == arr.length){ Student[] newArr = creatNewArr(arr); newArr[count] = stu4;
printArr(newArr);
}else{ arr[count] = stu4; 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()); } } }
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; }
public static boolean contains(Student[] arr, int id) { for (int i = 0; i < arr.length; i++) { Student stu = arr[i]; if(stu != null){ int sid = stu.getId(); if(sid == id){ return true; } } }
return false; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| package com.itheima.test8;
public class Test3 { public static void main(String[] args) {
Student[] arr = new Student[3]; Student stu1 = new Student(1, "zhangsan", 23); Student stu2 = new Student(2, "lisi", 24); Student stu3 = new Student(3, "wangwu", 25);
arr[0] = stu1; arr[1] = stu2; arr[2] = stu3;
int index = getIndex(arr, 2); if (index >= 0){ arr[index] = null; printArr(arr); }else{ System.out.println("当前id不存在,删除失败"); }
}
public static int getIndex(Student[] arr , int id){ for (int i = 0; i < arr.length; i++) { Student stu = arr[i]; 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()); } } }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package com.itheima.test8;
public class Test4 { public static void main(String[] args) {
Student[] arr = new Student[3]; Student stu1 = new Student(1, "zhangsan", 23); Student stu2 = new Student(2, "lisi", 24); Student stu3 = new Student(3, "wangwu", 25);
arr[0] = stu1; arr[1] = stu2; arr[2] = stu3;
int index = getIndex(arr, 2);
if(index >= 0){ Student stu = arr[index]; int newAge = stu.getAge() + 1; stu.setAge(newAge); printArr(arr); }else{ System.out.println("当前id不存在,修改失败"); }
}
public static int getIndex(Student[] arr , int id){ for (int i = 0; i < arr.length; i++) { Student stu = arr[i]; 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()); } } } }
|