news 2026/6/15 0:41:16

学习Java24天(练习)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
学习Java24天(练习)
import java.util.Random; public class Role { private String name; private int blood; private char gender; private String face; // 男性长相成语数组 String[] boyfaces = {"相貌堂堂", "眉清目秀", "气宇轩昂", "风度翩翩", "玉树临风"}; // 女性长相成语数组 String[] girlfaces = {"沉鱼落雁", "闭月羞花", "国色天香", "花容月貌", "倾国倾城"}; public Role(){} public Role(String name,int blood,char gender){ this.name = name; this.blood = blood; this.gender = gender; setFace(gender); } 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 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 void attack(Role role){ //计算造成的伤害 Random r = new Random(); int hurt = r.nextInt(20)+1; //修改挨揍人的血量 int remainBoold = role.getBlood() - hurt; //对剩余血量进行验证,如果为负数,就修改为0 remainBoold = remainBoold < 0 ? 0 : remainBoold; //修改一下挨揍人的血量 role.setBlood(remainBoold); //this表示方法的调用者 System.out.println(this.getName()+"举起拳头,打了"+role.getName()+"一下,造成了"+hurt+"点伤害,"+role.getName()+"还剩下"+remainBoold+"点血量"); } public void showRoleInfo(){ System.out.println("姓名为:"+getName()); System.out.println("血量为:"+getBlood()); System.out.println("性别为:"+getGender()); System.out.println("长相为:"+getFace()); } } 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开始攻击r2 r1.attack(r2); //判断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 Goods { private String id; private String name; private double pricce; private int count; public Goods(){} public Goods(String id,String name,double pricce,int count){ this.id = id; this.name = name; this.pricce = pricce; 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.id = name; } public double getPricce(){ return pricce; } public void setPricce(double pricce) { this.pricce = pricce; } public int getCount(){ return count; } public void setCount(int count) { this.count = count; } } public class GoodsTest { public static void main(String[] args) { Goods[] arr = new Goods[3]; Goods g1 = new Goods("goods001","手机",1999.0,50); Goods g2 = new Goods("goods002","电脑",6999.0,20); Goods g3 = new Goods("goods003","相机",9999.0,10); 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.getPricce()+","+goods.getCount()); } } }
public class Car { private String brand; private double price; private String color; public Car(){} public Car(String brand,double 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 double getPrice(){ return price; } public void setPrice(double price){ this.price = price; } public String getColor(){ return color; } public void setColor(String color){ this.color = color; } } import java.util.Scanner; public class CarText { 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("请输入第"+(i+1)+"辆汽车的品牌:"); String brand = Sc.next(); c.setBrand(brand); System.out.println("请输入第"+(i+1)+"辆汽车的价格:"); double price = Sc.nextDouble(); c.setPrice(price); System.out.println("请输入第"+(i+1)+"辆汽车的颜色:"); 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()); } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/8 9:52:31

从迷茫到精英:找到属于你的网络安全之路

一、为什么“自学”难以成就真正的网络安全工程师&#xff1f; 网络安全&#xff0c;从来不是一门可以“闭门造车”的学问。真正的攻防博弈&#xff0c;发生在真实的网络战场&#xff0c;而非虚拟的演示环境。一名合格的网络安全工程师&#xff0c;需要的是&#xff1a; 真实的…

作者头像 李华
网站建设 2026/6/13 22:31:10

Wan2.2-T2V-A14B能否生成符合人类视觉习惯的景深效果

Wan2.2-T2V-A14B能否生成符合人类视觉习惯的景深效果 在影视制作、广告创意乃至短视频内容爆发的今天&#xff0c;观众对视频“真实感”的要求早已超越了画面清晰和动作连贯。一个镜头是否具备自然的空间层次——比如前景人物清晰锐利&#xff0c;背景城市灯光柔和弥散成光斑—…

作者头像 李华
网站建设 2026/6/7 12:51:50

金融机构如何落地智能体?16个头部企业Agent最佳实践

文章介绍了金融业智能体(AI Agent)的定义、特征及应用进展。智能体具有自主性和学习能力&#xff0c;正被银行、证券、保险等金融机构广泛采纳。文章分析了智能体在金融领域的应用场景及面临的挑战&#xff0c;并提供了多个金融机构的智能体应用案例&#xff0c;为金融业智能体…

作者头像 李华
网站建设 2026/6/14 13:25:22

基于大数据的校园美食推荐系统的设计与实现scrapy+hadoop

文章目录项目简介系统截图大数据系统开发流程主要运用技术介绍参考文献结论源码文档获取定制开发/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;项目简介 本系统基于校园餐饮场景需求&#xff0c;采用模块化设计理念构建了完整的用户端和管理端功能…

作者头像 李华
网站建设 2026/6/12 3:09:15

探索双馈风力发电系统的双PWM变换器控制

双馈风力发电系统&#xff0c;双pwm变换器控制系统&#xff0c;采用直接转矩输入代替风力发电机。 &#xff08;1&#xff09;转子侧采用基于定子磁链定向的矢量控制策略&#xff0c;对d轴进行定向&#xff0c;采用双闭环控制结构&#xff0c;外环为速度环&#xff0c;内环为电…

作者头像 李华