当前位置: 首页 > news >正文

学习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()); } } }
http://www.cnnetsun.cn/news/9873.html

相关文章:

  • 2023年IEEE TIV,GA-LNS算法+直升机救援调度,深度解析+性能实测
  • xshell的一个会话的连接的ip地址在哪里修改?
  • 【活动总结】创药沙龙第一期:ADC药物研发的挑战与机遇成功举办
  • 如何用免费工具3分钟终极优化Windows右键菜单:告别杂乱,提升300%操作效率
  • Day25
  • 工具 | netcat, netstat
  • AI的下半场:智能体(Agent)将如何重塑我们所有的应用
  • soular全面介绍(4) - 通过soular工作台聚合TikLab所有工具链
  • R-Zero:从零数据自进化推理大语言模型
  • 弹~性布局
  • Wan2.2-T2V-A14B在地震波传播模拟教学中的科学准确性
  • Day 36 MLP神经网络的训练
  • B站视频下载终极指南:免费工具DownKyi完整使用教程
  • 搞懂“元数据”:给数据办一张“身份证”
  • 04_C 语言进阶之避坑指南:多重 if-else 及多重条件混乱 —— 让逻辑不再 “绕迷宫”
  • 量子计算开发者必看(VSCode性能调优实战手册)
  • Android嵌套滑动冲突完全解析:从原理到实战解决方案
  • ASTM D4169-DC13 标准,包装完整性
  • Linux新手必学:tail命令图解指南
  • 19、利用Scapy和Python进行网络数据包处理与扫描
  • 性能测试里MySQL的锁
  • OBS教程:OBS实时字幕插件如何下载?直播字幕翻译怎么弄?
  • MagicTime: Time-Lapse Video Generation Models asMetamorphic Simulators论文精读(1)
  • Laravel 13多模态表单处理:从入门到精通的6大实战场景,错过等于失业
  • 读捍卫隐私03同步
  • [Android] B站第三方电视TVapp BV_0.3.10
  • 【time-rs】 time-core crate 的 Cargo.toml 配置文件详解
  • 政府网站与政务新媒体考核指标有什么区别
  • FLUX.1 Kontext终极指南:重新定义AI图像编辑的边界
  • Java新手必看:System类为什么会出现安全警告?