面向对象 练习 11.14

package com.itheima;

import java.util.Random;

public class Role1 {
    private String name;
    private int blood;

    //空参
    public Role1() {
    }
    //

    public Role1(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }

    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 void attack(Role1 role){
        //随机生成血量
        Random r=new Random();
        int hurt=r.nextInt(20);
        //剩余血量
        int remainblood=role.getBlood()-hurt;
        remainblood=remainblood<0?0:remainblood;


        role.setBlood(remainblood);
        System.out.println(this.getName()+"举起了拳头,打了"+this.getName() +" 一下,造成了"+hurt+"点伤害" +
               this.getName()+ " 还剩下"+remainblood+" 血量");

    }




}

package com.itheima;

public class gametest1 {
    public static void main(String[] args) {
        Role1 r1=new Role1("zero",100);
        Role1 r2=new Role1("daxian",100);


        while(true){
            r1.attack(r2);
            if(r1.getBlood()==0){
                System.out.println(r2.getName()+" ko了"+r1.getName());
                break;
            }
            r2.attack(r1);
            if(r2.getBlood()==0){
                System.out.println(r1.getName()+" ko了"+r2.getName());
                break;
            }

        }


    }
}

字符串

package com.itheima;

import java.util.Scanner;

//键盘录入一个字符串,统计大写字母,小写,数字出现的次数
public class Stringdemo4 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String str=sc.nextLine();
        //统计数据

    int bigcount=0;
    int smallcount=0;
    int numbercount=0;

    //遍历
        for(int i=0;i<str.length();i++){
            char c=str.charAt(i); //**
            if(c>='0' && c<='9'){
                numbercount++;
            }else if(c>='a' && c<='z'){
                smallcount++;
            }else if(c>='A' && c<='Z'){
                bigcount++;
            }
        }

        System.out.println("大写字母是"+bigcount+"个");
        System.out.println("小写字母是"+smallcount+"个");
        System.out.println("数字是"+numbercount+"个");



    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值