文章目录
面向对象综合案例
1.文字版格斗游戏
格斗游戏,每个游戏角色的姓名、血量都不相同,在选定人物(new对象的时候),这些信息就应该被确定下来。
(1)基础代码实现
//Javabean类
import java.util.Random;
public class GameCharacter{
private String name;
private int blood;
//空参构造方法
public GameCharacter(){
}
//带全部参数的构造方法
public GameCharacter(String name,int blood){
this.name = name;
this.blood = blood;
}
public void setName(String name){
this.name = name;
}
//每个成员变量的get和set方法
public String getName(){
return name;
}
public void setBlood(int blood){
this.blood = blood;
}
public int getBlood(){
return blood;
}
public void attack(GameCharacter role) {
Random r = new Random();
int hunt = r.nextInt(20) + 1;
int remain = role.getBlood();
remain -= hunt;
remain = remain > 0 ? remain : 0;
role.setBlood(remain);
if(remain > 0){
System.out.println(this.getName() + " attacked " + role.getName() + " causing " + hunt + " damage." + role.getName() + " still have " + remain + " blood left.");
}
}
//测试类
public class GameCharacterText{
public static void main(String[] args){
GameCharacter gc1 = new GameCharacter("Harry Potter",100);
GameCharacter gc2 = new GameCharacter("Voldemort",100);
while(gc1.getBlood() > 0 && gc2.getBlood() > 0) {
gc1.attack(gc2);
gc2.attack(gc1);
if(gc1.getBlood() == 0 ) {
System.out.println(gc2.getName() + " KO " + gc1.getName());
break;
}
if(gc2.getBlood() == 0 ) {
System.out.println(gc1.getName() + " KO " + gc2.getName());
break;
}
}
}
}
(2)printf (快捷键: souf + 回车)
//%s占位
System.out.printf("Hello!%s","Linda");//写两个printf,没有换行效果,会在一行输出。可以在之间加一个System.out.println();
(3)丰富格斗过程
//Javabean类
import java.util.Random;
public class GameCharacter{
private String name;
private int blood;
private String gender;
private String face;
//定义一个男性相貌数组和一个女性相貌数组,通过随机索引,给两个对象的face赋值
//空参构造方法
public GameCharacter(){
}
//带全部参数的构造方法
public GameCharacter(String name,int blood){
this.name = name;
this.blood = blood;
}
public void setName(String name){
this.name = name;
}
//每个成员变量的get和set方法
public String getName(){
return name;
}
public void setBlood(int blood){
this.blood = blood;
}
public int getBlood(){
return blood;
}
public void setGender(String gender){
this.gender = gender;
}
public String getGender(){
return gender;
}
String [] maleface = {"风流倜傥","气宇轩昂","相貌英俊","五官端正","相貌平平","一塌糊涂","面目狰狞"};
String [] femaleface ={"天仙下凡","沉鱼落雁","亭亭玉立","身材姣好","相貌平平","相貌简陋","惨不忍睹"};
public void setFace(String gender){
Random r = new Random();
if(gender == "男"){
int mindex = r.nextInt(maleface.length);
this.face = maleface[mindex];
} else{
int findex= r.nextInt(femaleface.length);
this.face = femaleface[findex];
}
}
public String getFace(String face){
return face;
}
String [] attack = {"%s使出一招【背心钉】,转到对方身后,一掌向%s背心的灵台穴拍去","%s使出一招【游空探爪】,飞起身形自半空中变掌为爪锁向%s","%s大喝一声,身形下伏,一招【霹雷坠地】,锤向%s双腿","%s运气于掌,一瞬间掌心变得血红,一式【掌心雷】,推向%s","%s阴手翻起阳手跟进,一招【没遮拦】,结结实实地锤向%s","%s上步抢身,招中套招,一招【披挂连环】,连环攻向%s"};
String [] injure = {
"结果%s退了半步,毫发无损",
"结果给%s造成一处瘀伤",
"结果一击命中,%s痛得弯下腰",
"结果%s痛苦地闷哼了一声,显然受了点内伤",
"结果%s摇摇晃晃,一跤摔倒在地",
"结果%s脸色一下变得惨白,连退了好几步",
"结果轰的一声,%s鲜血狂喷而出",
"结果%s一声惨叫,像滩软泥般塌了下去"};
public void attack(GameCharacter role) {
Random r = new Random();//可以只在Javabean类的开头只写一遍
int hunt = r.nextInt(20) + 1;
int remain = role.getBlood();
remain -= hunt;
remain = remain > 0 ? remain : 0;
role.setBlood(remain);
int index = r.nextInt(attack.length);
System.out.printf(attack[index] , this.getName() ,role.getName() );//attack[index]中有占位符%s,在后面按顺序写上占位符里要填充的内容
if(remain >= 90){
System.out.printf(injure[0],role.getName());
} else if(remain >= 80){
System.out.printf(injure[1],role.getName());
}else if(remain >= 70){
System.out.printf(injure[2],role.getName());
}else if(remain >= 60){
System.out.printf(injure[3],role.getName());
} else if(remain >= 40 ){
System.out.printf(injure[4],role.getName());
} else if(remain >= 20){
System.out.printf(injure[5],role.getName());
}else if(remain >= 10){
System.out.printf(injure[6],role.getName());
} else {
System.out.printf(injure[7],role.getName());
}
System.out.println();
}
}
//测试类
public class GameCharacterText{
public static void main(String[] args){
GameCharacter gc1 = new GameCharacter("乔峰",100);
GameCharacter gc2 = new GameCharacter("鸠摩智",100);
while(gc1.getBlood() > 0 && gc2.getBlood() > 0) {
gc1.attack(gc2);
gc2.attack(gc1);
if(gc1.getBlood() == 0 ) {
System.out.println(gc2.getName() + " KO " + gc1.getName());
break;
}
if(gc2.getBlood() == 0 ) {
System.out.println(gc1.getName() + " KO " + gc2.getName());
break;
}
}
}
}
2.对象数组练习
(1)对象数组1
定义数组存储3个商品对象
商品的属性:商品的id、名字、价格、库存
创建三个商品对象,并把商品对象存入数组当中。
//Javabean类
public class Commodity{
private String id;
private String name;
private double price;
private int left;
public Commodity(){
}
public Commodity(String id,String name,double price,int left){
this.id = id;
this.name = name;
this.price = price;
this.left = left;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setPrice(double price){
this.price = price;
}
public double getPrice(){
return price;
}
public void setLeft(int left){
this.left = left;
}
public int getLeft(){
return left;
}
}
测试类
public class CommodityText{
public static void main(String[] args){
Commodity [] good = new Commodity[3];//创建一个数组
//创建三个对象
Commodity c1 = new Commodity("001","华为",8999.9,100);
Commodity c2 = new Commodity("002","红米",5899.0,89);
Commodity c3 = new Commodity("003","荣耀",5689.7,108);
good[0] = c1;//将第一个对象的地址值,赋值给0索引的内容
good[1] = c2;//将第二个对象的地址值,赋值给1索引的内容
good[2] = c3;//将第三个对象的地址值,赋值给2索引的内容
for (int i = 0; i < good.length; i++) {
System.out.println(good[i].getId() + " " +good[i].getName() + " " +good[i].getPrice() + " " + good[i].getLeft());
}
}
}
(2)对象数组2
定义数组存储3部汽车对象
汽车的属性:品牌、价格、颜色
创建三个汽车对象,数据通过键盘录入而来,并把数据存入数组当中。
//Javabean类
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 void setBrand(String brand){
this.brand = brand;
}
public String getBrand(){
return brand;
}
public void setPrice(int price){
this.price = price;
}
public int getPrice(){
return price;
}
public void setColor(String color){
this.color = color;
}
public String getColor(){
return color;
}
}
//测试类
import java.util.Scanner;
public class CarText{
public static void main(String[] args){
Car [] cars = new Car [3];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < cars.length; i++) {
Car c = new Car();//先根据类创建对象,再用set方法将键盘录入的值赋值给所创建的对象的成员变量。要将对象的创建放在循环里面!
System.out.println("请输入第" + (i + 1) + "辆汽车的品牌");
c.setBrand(sc.next());
System.out.println("请输入第" + (i + 1) + "辆汽车的价格");
c.setPrice(sc.nextInt());
System.out.println("请输入第" + (i + 1) + "辆汽车的颜色");
c.setColor(sc.next());
cars[i] = c;//如果将对象的创建放在循环的外面,每一修改的都是同一个对象的成员变量,那么最后遍历的结果都是最后一次修改的内容
}
for(int i = 0;i < cars.length;i++){
System.out.println(cars[i].getBrand() + " " + cars[i].getPrice() + " " + cars[i].getColor());
}
}
}
键盘录入
①第一套体系
遇到空格、制表符、回车就停止接收,这些符号后面的数据就不会接收
nextInt();接收整数
nextDouble();接收小数
next();接收字符串
import java.util.Scanner;
public class Demo1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first number.");
int num1 = sc.nextInt();
System.out.println("Please enter the second number.");
int num2 = sc.nextInt();
}
}
控制台提示输入第一个数字,当输入“123 123”后,控制台输出了接收到的第一个123后,不会等待输入,而是直接输出提示语句和第二个123。
②第二套体系
nextLine();
接收字符串,可以接收空格和制表符,遇到回车停止接收数据。
③键盘录入的两套体系不能混用
import java.util.Scanner;
public class Demo2{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a integer.");//123 + 回车
int num = sc.nextInt();//num接收了123
System.out.println(num);
System.out.println("Please enter a character string.");
String line = sc.nextLine();//寄存器里还有一个回车,被line接收了
System.out.println(line);
}
}
(3)对象数组3
定义数组存储3部手机对象
手机的属性:品牌、价格、颜色
要求,计算出三部手机的平均价格
//Javabean类
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 void setBrand(String brand){
this.brand = brand;
}
public String getBrand(){
return brand;
}
public void setPrice(int price){
this.price = price;
}
public int getPrice(){
return price;
}
public void setColor(String color){
this.color = color;
}
public String getColor(){
return color;
}
}
//测试类
import java.util.Scanner;
public class PhoneText{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Phone [] phone = new Phone [3];
int sum = 0;
double aver = 0.0;
for(int i = 0;i < phone.length;i++){
Phone p = new Phone();
System.out.println("请输入第"+( i + 1 )+ "部手机的品牌");
p.setBrand(sc.next());
System.out.println("请输入第"+( i + 1 )+ "部手机的价格");
p.setPrice(sc.nextInt());
sum += p.getPrice();
System.out.println("请输入第"+( i + 1 )+ "部手机的颜色");
p.setColor(sc.next());
}
aver = sum * 1.0 / phone.length;//BigDecimal
System.out.println("三部手机的平均价格为" + aver);
}
}
(4)对象数组4
定义数组存储4个女朋友的对象
女朋友的属性:姓名、年龄、性别、爱好
要求1:计算出5个女朋友的平均年龄
要求2:统计年龄比平均年龄低的女朋友有几个?并把她们的所有信息打印出来。
//Javabean类
public class GirlFriend{
private String name;
private int age;
private String gender;
private String hobby;
public GirlFriend(){
}
public GirlFrienf(String name,int age,String gender,String hobby){
this.name = name;
this.age = age;
this.gender = gender;
this.hobby = hobby;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void setGender(String gender){
this.gender = gender;
}
public String getGender(){
return gender;
}
public void setHobby(String hobby){
this.hobby = hobby;
}
public String getHobby(){
return hobby;
}
}
//测试类
import java.util.Scanner;
public class GirlFriendText{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
GirlFriend [] girlf = new GirlFriend [4];
int sum = 0;
double aver = 0.0;
int count = 0;
for(int i = 0;i < girlf.length;i++){
GirlFriend gf = new GirlFriend();
System.out.println("请输入第" + ( i + 1 ) + "个女朋友的姓名");
gf.setName(sc.next());
System.out.println("请输入第" + ( i + 1 ) + "个女朋友的年龄");
gf.setAge(sc.nextInt());
sum += gf.getAge();
System.out.println("请输入第" + ( i + 1 ) + "个女朋友的性别");
gf.setGender(sc.next());
System.out.println("请输入第" + ( i + 1 ) + "个女朋友的爱好");
gf.setHobby(sc.next());
girlf[i] = gf;
}
aver = sum * 1.0 / girlf.length;
System.out.println("四个女朋友的平均年龄为" + aver);
for(int i = 0;i < girlf.length;i++){
if(girlf[i].getAge() < aver){
count++;
System.out.println("姓名:" + girlf[i].getName() + " " + "年龄:" + girlf[i].getAge() + " " + "性别:" + girlf[i].getGender() + " " + "爱好:" + girlf[i].getHobby());
}
}
System.out.println("有" + count + "个女朋友比平均年龄低");
}
}
(5)对象数组5(待修改)
定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号、姓名各不相同。
学生的属性:学号、姓名、年龄。
要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一判断。
要求2:添加完毕之后,遍历所有学生信息。
要求3:通过id删除学生信息
如果存在,则删除;如果不存在,则提示删除失败。
要求4:删除完毕之后,遍历所有学生信息。
要求5:查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁。
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 void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
import java.util.Scanner;
public class StudentText {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student[] stus = new Student[3];
int i, j;
Student stu1 = new Student(1, "张三", 19);
Student stu2 = new Student(2, "李四", 20);
Student stu3 = new Student(3, "王五", 18);
Student s = new Student();
System.out.println("请输入第待添加的学生的学号");
int tmp = sc.nextInt();
if (!isIdExist(stus,tmp)) {
s.setId(tmp);
System.out.println("请输入第待添加的学生的姓名");
s.setName(sc.next());
System.out.println("请输入第待添加的学生的年龄");
s.setAge(sc.nextInt());
System.out.println("添加成功!");
} else {
System.out.println("添加失败,该学生已存在");
}
int count = arrCount(stus);
if (count == stus.length) {
Student[] studs = creatNewArr(stus);
studs[count] = s;
System.out.println("添加后");
printArr(studs);
studs = deleteArr(studs);
System.out.println("删除后");
printArr(studs);
for (i = 0; i < studs.length; i++) {
if (002 == studs[i].getId()) {
stus[i].setAge(studs[i].getAge() + 1);
;
}
}
System.out.println("修改后");
printArr(studs);
} else {
stus[count] = s;
System.out.println("添加后");
printArr(stus);
stus = deleteArr(stus);
System.out.println("删除后");
printArr(stus);
for (i = 0; i < stus.length; i++) {
if (002 == stus[i].getId()) {
stus[i].setAge(stus[i].getAge() + 1);
;
}
}
System.out.println("修改后");
printArr(stus);
}
public static boolean isIdExist(Student[] stu,int id){
for ( i = 0; i < stu.length; i++) {
if (id == stu[i].getId()) {
return true;
}
}
return false;
}
public static int arrCount (Student[]stu){
count = 0;
for ( i = 0; i < stu.length; i++) {
if (stu[i] != null) {
count++;
}
}
return count;
}
public static Student[] creatNewArr(Student[]stu){
Student[] studs = new Student[stu.length + 1];
for (i = 0; i < stu.length; i++) {
studs[i] = stu[i];
}
return studs;
}
public static void printArr (Student[]stu){
for (int i = 0; i < stu.length; i++) {
if (stu[i] != null) {
System.out.println("第" + (i + 1) + "个学生学号:" + stu[i].getId() + " " + "姓名:" + stu[i].getName() + " " + "年龄:" + stu[i].getAge());
}
}
}
public static Student[] deleteArr(Student[] stu){
System.out.println("请输入待删除的学生学号");
for (i = 0; i < stu.length; i++) {
if (isIdExist(stu, sc.nextInt())) {
if (i == stu.length - 1) {
stu[stu.length - 1] = null;
} else {
for (j = i; j < stu.length - 1; j++) {
stu[j] = stu[j + 1];
}
stu[stu.length - 1] = null;
}
} else {
System.out.println("学生不存在,删除失败!");
}
}
return stu;
}
}
}