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+"个");
}
}
3989

被折叠的 条评论
为什么被折叠?



