Java--面向对象经典练习题

文章内容

一、文字版格斗游戏

二、对象数组1

三、对象数组2

四、对象数组3

五、对象数组4

题目代码如下:

文字版格斗游戏

格斗游戏,每个游戏角色的姓名,血量,都不相同,再选定人物的时候(new对象的时候),这些信息就应该被确定下来,例如:

 

import java.util.Random;
 
public class Role {
	private String name;
	private int blood;
	
	public Role() {
		
	}
	public Role(String name,int blood) {
		this.name=name;
		this.blood=blood;
	} 
	public void setName(String name) {
		this.name=name;
	}
	public String getName() {
		return name;
	}
	public void setBlood(int blood) {
		this.blood=blood;
	}
	public int getBlood() {
		return blood;
	}
	public void showRoleInfo() {
		System.out.println("姓名为:"+getName());
		System.out.println("血量为:"+getName());
		System.out.println("性别为:"+getName());
		System.out.println("长相为:"+getName());
		
	}
	public  void attack(Role role) {
		//计算造成的伤害(1~20)
		Random r=new Random();
		int hurt=r.nextInt(20)+1;
		
		//剩余血量
		int remainBlood=role.getBlood()-hurt;
		//对剩余血量做一个验证,如果为负数,就修改为0
		remainBlood=remainBlood < 0 ? 0:remainBlood;
		//修改一下挨揍人的血量
		role.setBlood(remainBlood);
		
		//this表示方法的调用者
		System.out.println(this.getName()+"举起拳头打了"+role.getName()+"一下,"
				+ "造成了"+hurt+"点伤害,"+role.getName()+"还剩下"+remainBlood+"点血量");
	}
 
}
public class GameTest {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//创建第一个角色
		Role r1=new Role("乔峰",100);
		//创建第二个角色
		Role r2=new Role("鸠摩智",100);
		
		//开始格斗  回合制游戏
		while(true) {
			r2.attack(r1);
			if(r1.getBlood()==0) {
				System.out.println(r2.getName()+"K.O了"+r1.getName());
				break;
			}
			
			r1.attack(r2);
			if(r2.getBlood()==0) {
				System.out.println(r1.getName()+"K.O了"+r2.getName());
				break;
			}
		}
		
 
	}
 
}

对象数组1

需求:

定义数组存储三个商品对象。

商品的属性:商品的id,名字,价格,库存。

创建三个商品对象,并把商品对象放到数组当中。

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 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(int price) {
		this.price=price;
	}
	
	public double getPrice() {
		return price;
	}
	
	public void setCount(int count) {
		this.count=count;
	}
	
	public int getCount() {
		return count;
	}
 
}
public class TestGoods {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.创建一个数组
		Goods arr[]=new Goods[3];
		
		//2.创建三个商品对象
		Goods g1=new Goods("001","华为p40",5999.0,100);
		Goods g2=new Goods("002","保温杯",277.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++) {
			Goods good=arr[i];
			System.out.println(good.getId()+", "+good.getName()+", "+good.getPrice()+", "+good.getCount());
		}
		
 
	}
 
}

对象数组2

定义数组存储3部汽车对象。

汽车的属性:品牌、价格、颜色。

创建三个汽车对象,数据通过键盘录入而来,并把数据存放到数组当中。


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 TestCar {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.定义一个数组
		Car arr[]=new Car[3];
		
		
		
		Scanner sc=new Scanner(System.in);
		for(int i=0;i<arr.length;i++) {
			//2.创建三个汽车对象
			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

定义数组存储3部手机对象。

手机的属性:品牌、价格、颜色。

要求计算出三部手机的平均价格

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;
	}

}
public class TestPhone {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//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++) {
			Phone phone=arr[i];
			//获得每个手机的价格
			sum+=phone.getPrice();
		}
		//5.计算手机的平均值
		int avg=sum/arr.length;
		System.out.println("手机的平均值为:"+avg);

	}

}

对象数组4

定义数组存储4位学生对象。

学生的属性:姓名、年龄、姓名、爱好。

统计年龄比平均值低的学生有几个?并把他们的所有信息都打印出来

public class Student {
	private String name;
	private int age;
	private String gender;
	private String hobby;
	
	public Student() {
		
	}
	
	public Student(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;
	}

}
public class TestStudent {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.创建学生数组
		Student arr[]=new Student[4];
		
		//2.创建学生对象
		Student s1=new Student("小林",19,"男","打游戏");
		Student s2=new Student("小惠",20,"女","看书");
		Student s3=new Student("小单",21,"男","写作业");
		Student s4=new Student("小诗",22,"女","学习");
		
		//3.把对象添加到数组当中
		arr[0]=s1;
		arr[1]=s2;
		arr[2]=s3;
		arr[3]=s4;
		
		int sum=0;
		for(int i=0;i<arr.length;i++) {
			Student stu=arr[i];
			//获得每个手机的价格
			sum+=stu.getAge();
		}
		
		int avg=sum/arr.length;
		System.out.println("平均年龄为:"+avg);
		
		int count=0;
		for(int i=0;i<arr.length;i++) {
			Student stu=arr[i];
			int age=stu.getAge();
			if(age<avg) {
				count++;
				System.out.println(stu.getName()+", "+stu.getAge()+", "+stu.getGender()+", "+stu.getHobby());
			}
			
		}
		System.out.println("比平均值低的学生有"+count+"个");

	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值