news 2026/5/26 7:36:12

学习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/5/26 1:06:18

系统提示找不到msvcp140_codecvt_ids.dll文件 无法运行程序 下载修复

在使用电脑系统时经常会出现丢失找不到某些文件的情况&#xff0c;由于很多常用软件都是采用 Microsoft Visual Studio 编写的&#xff0c;所以这类软件的运行需要依赖微软Visual C运行库&#xff0c;比如像 QQ、迅雷、Adobe 软件等等&#xff0c;如果没有安装VC运行库或者安装…

作者头像 李华
网站建设 2026/5/26 4:20:39

超细整理,性能测试如何做?怎么做?性能压力负载(汇总二)

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 性能测试结果分析…

作者头像 李华
网站建设 2026/5/26 2:41:58

C++的第十四天笔记

存储持续性、作用域和链接性C使用三种&#xff08;C11四种&#xff09;不同方案存储数据。这些方案的区别在于数据保留在内存中的时间。自动存储持续性&#xff1a;在函数定义中声明的变量&#xff08;包括函数参数&#xff09;&#xff0c;程序执行所属函数 / 代码块时创建&am…

作者头像 李华
网站建设 2026/5/25 14:46:15

874-LangChain框架Use-Cases - 基于智能体的动态槽位填充系统 - 案例分析

1. 案例目标 本案例旨在构建一个基于智能体的动态槽位填充系统&#xff0c;实现智能对话系统&#xff0c;能够分析用户请求并自动收集必要信息&#xff0c;通过对话补充缺失信息。 系统主要实现以下目标&#xff1a; 实现动态槽位填充功能&#xff0c;自动识别并收集必要信息…

作者头像 李华
网站建设 2026/5/26 2:43:10

ops-nn算子库生态纵览 - 构建健壮的AI算力基石

目录 &#x1f3af; 摘要 1. ops-nn&#xff1a;CANN神经网络计算的中枢神经系统 1.1 &#x1f504; 算子库的定位与演进轨迹 1.2 &#x1f4ca; 矩阵计算&#xff1a;AI算力的本质洞察 2. NPU硬件架构&#xff1a;算子设计的物理基础 2.1 &#x1f527; AI Core微架构深…

作者头像 李华