news 2026/6/13 23:27:39

学习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/11 0:01:17

Wan2.2-T2V-A14B如何处理遮挡关系以增强空间感

Wan2.2-T2V-A14B如何处理遮挡关系以增强空间感 在当前AI生成内容迈向“动态世界构建”的关键时刻&#xff0c;一个看似细微却极为关键的挑战浮出水面&#xff1a;当一个人物从树后走出、一辆车驶过行人前方、一只鸟飞入建筑阴影中——这些日常场景中的遮挡与重现&#xff0c;恰…

作者头像 李华
网站建设 2026/6/10 16:03:19

你真的会写Agentic Apps配置吗?Docker Compose中不可不知的4大陷阱与优化

第一章&#xff1a;Docker Compose 中 Agentic Apps 的定义与配置概述在现代云原生应用开发中&#xff0c;Agentic Apps 指具备自主行为能力、可感知环境并作出响应的分布式服务组件。这类应用通常由多个协同工作的微服务构成&#xff0c;每个服务都封装了特定的业务逻辑&#…

作者头像 李华
网站建设 2026/6/12 16:45:37

华为HCIA笔记——第十三天

生成树协议 本章内容详细介绍了STP(生成树协议)的产生背景、工作原理和RSTP等内容 生成树协议 一、STP协议 1.1 产生背景 1.2 STP基本概念 1.3 BPDU报文格式 1.4 STP拓扑计算 二、RSTP协议 2.1 STP的不足 2.2 RSTP对STP的改进 三、生成树技术进阶 一、STP协议 1.1 产生背景…

作者头像 李华
网站建设 2026/6/12 18:59:01

COO格式稀疏矩阵进行Permutation置换操作

文章目录✅ 步骤 1&#xff1a;应用对称置换✅ 步骤 2&#xff1a;构造逆排列✅ 步骤 3&#xff1a;应用置换并排序 COO&#x1f501; 如何“逆置换”&#xff1f;✅ 补充说明要对一个 COO 格式&#xff08;Coordinate Format&#xff09;的稀疏方阵A∈RnnA \in \mathbb{R}^{n …

作者头像 李华
网站建设 2026/6/13 1:33:06

全国大学生数据建模比赛精讲系列——时间序列(详细解读)

全国大学生数学建模竞赛:时间序列分析方法全解析 时间序列分析是全国大学生数学建模竞赛中解决动态数据问题的核心方法之一,广泛应用于经济预测、销量分析、环境监测等场景。本文从概念、流程、实操等维度,系统拆解时间序列分析在建模竞赛中的应用逻辑,并结合实战案例给出可…

作者头像 李华
网站建设 2026/6/12 19:22:56

我发现自监督学习补全EHR缺失字段,基层慢病管理预警准度飙升

&#x1f4dd; 博客主页&#xff1a;Jax的CSDN主页 目录当AI成了我的“医疗小助手”&#xff1a;那些令人哭笑不得的诊断日常 一、从“病历录入机”到“AI急诊室” 二、AI诊断的“薛定谔的正确” 三、AI在乡村的“降维打击” 四、AI的“道德困境”时刻 五、未来已来&#xff1f…

作者头像 李华