news 2026/7/14 15:24:41

学习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/7/13 16:59:46

2023年IEEE TIV,GA-LNS算法+直升机救援调度,深度解析+性能实测

目录1.摘要2.航空救援路线问题&#xff08;ARRP&#xff09;3.GA-LNS算法4.结果展示5.参考文献6.代码获取7.算法辅导应用定制读者交流1.摘要 本文研究多直升机、多往返任务的航空救援路径规划问题&#xff08;ARRP&#xff09;&#xff0c;弥补了以往研究忽略实际救援流程与燃…

作者头像 李华
网站建设 2026/7/13 11:47:02

xshell的一个会话的连接的ip地址在哪里修改?

在 Xshell 中修改已有会话的 IP 地址&#xff08;主机地址&#xff09;非常简单&#xff0c;请按照以下步骤操作&#xff1a;打开会话管理器 打开 Xshell&#xff0c;在左侧通常会有一个“会话管理器”栏&#xff08;如果没有显示&#xff0c;可以点击顶部菜单栏的 “查看” (V…

作者头像 李华
网站建设 2026/7/13 5:57:25

【活动总结】创药沙龙第一期:ADC药物研发的挑战与机遇成功举办

2025年12月5日&#xff0c;创药沙龙第一期在中国科学院上海药物研究所海科路园区成功举办。本期沙龙在中国科学院上海药物研究所校友会与中南大学浙江校友会的共同指导下&#xff0c;由恺思俱乐部、杏林泛舟联合主办&#xff0c;杏香雅集、恺思学社协办。活动以“ADC药物研发的…

作者头像 李华
网站建设 2026/7/13 20:36:48

Day25

疏锦行

作者头像 李华
网站建设 2026/7/14 1:08:04

工具 | netcat, netstat

1. netcat&#xff1a;监听目标端口&#xff0c;查看接收的数据&#xff08;reverse shell的实现利用&#xff09;&#xff1b; 2. netstat&#xff1a;监听本机的网络连接情况&#xff0c;不知道网络连接端口时可以使用netstat&#xff1b; 1. netcat 功能&#xff1a;监听网…

作者头像 李华