news 2026/1/15 7:54:55

学习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进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/12 0:29:15

日期题模版(made by yyf)

日期题通常包括&#xff1a;判断是否为闰年&#xff0c;计算某年某月有多少天&#xff0c;日期自增&#xff0c;遍历日期等&#xff0c;这里给出总结判断是否为闰年首先什么是闰年&#xff0c;闰年具有哪些特征&#xff1f;如果是整百年&#xff08;如2000&#xff0c;1700&…

作者头像 李华
网站建设 2026/1/1 10:34:19

CppCon 2024 学习:Gazing Beyond Reflection for C++26

1⃣ 第一个片段 源语言构造&#xff08;语法域&#xff09; ^^std::vector<int> ^^int(*)() ^^std::cout ^^std::vector ^^std ^^::理解 这里的 ^^ 是一个元语言标记&#xff0c;表示这是语法结构的占位符&#xff0c;即“元变量”。它们代表的是代码中的语法单元&…

作者头像 李华
网站建设 2026/1/13 0:32:08

c++--_

map

作者头像 李华
网站建设 2025/12/30 8:00:01

Day9 >> 151、反转字符串中的单词 +

代码随想录字符串部分 151、反转字符串中的单词 这道题感觉还挺难的&#xff0c;而且好多种解法啊&#xff0c;Java版本总共给了4种解法&#xff0c;先挑了其中一个较容易理解的解法练习了一遍。 练习的过程中&#xff0c;写删除多余空格方法时&#xff0c;把 while 判断条件…

作者头像 李华
网站建设 2026/1/10 8:10:16

三星三折叠价格和功能揭秘:19999元起,三折叠旗舰藏多少惊喜?

万元级折叠屏市场再添重磅选手&#xff0c;三星GalaxyZTriFold以19999元起售的定价登场&#xff0c;其功能配置是否能匹配高端定位&#xff1f;今天我们就聚焦“三星三折叠价格和功能”&#xff0c;拆解这款旗舰的价值内核。价格定位清晰&#xff1a;双版本覆盖高端需求关于消费…

作者头像 李华
网站建设 2026/1/11 19:15:15

探索近乎完全消光的圆二色超反射镜与圆偏振光设置

近乎完全消光的圆二色超反射镜 圆偏振光的设置在光学领域&#xff0c;圆二色超反射镜以及圆偏振光的相关研究一直是极具吸引力的方向。今天咱们就来聊聊近乎完全消光的圆二色超反射镜以及圆偏振光的设置那些事儿。 近乎完全消光的圆二色超反射镜 圆二色性&#xff08;CD&#…

作者头像 李华