本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
程序实现
1.创建角色姓名
public void getname() {
System.out.print("请输入角色名字: ");
name = sc.nextLine();
}
public void getsex() {
int i;
System.out.print("请选择角色性别 (0.男 1.女): ");
i=sc.nextInt();
if(i==0)
sex="男";
else if(i==1)
sex="女";
else
System.out.println("输入有误!");
}
2.创建种族,对每个种族可以选择的职业确定范围
public void getrace() {
int i;
System.out.print("请选择角色种族 ( 0.人类 1.精灵 2.兽人 3.矮人 4.元素): ");
i= sc.nextInt();
if(i==0) {
race = "人类";
kuangzhanshi = true;
shengqishi = true;
cike = true;
lieshou = true;
jisi = true;
wushi = true;
}
else if(i==1) {
race = "精灵";
kuangzhanshi = false;
shengqishi = false;
cike = true;
lieshou = true;
jisi = true;
wushi = true;
}
else if(i==2) {
race = "兽人";
kuangzhanshi = true;
shengqishi = false;
cike = false;
lieshou = true;
jisi = true;
wushi = false;
}
else if(i==3) {
race = "矮人";
kuangzhanshi = true;
shengqishi = true;
cike = false;
lieshou = false;
jisi = true;
wushi = false;
}
else if(i==4) {
race = "元素";
kuangzhanshi = false;
shengqishi = false;
cike = false;
lieshou = false;
jisi = true;
wushi = true;
}
else {
System.out.println("输入有误,请重新选择!");
getrace();
}
}
- 创建职业,并根据职业特点对各属性随机赋值。
public void getprofession() {
System.out.println("请选择您的职业(0.狂战士 1.圣骑士 2.刺客 3.猎手 4.祭司 5.巫师):");
int i=sc.nextInt();
if(i==0) {
creatkuangzhanshi(kuangzhanshi);
}
if(i==1) {
creatshengqishi(shengqishi);
}
if(i==2) {
creatcike(cike);
}
if(i==3) {
creatlieshou(lieshou);
}
if(i==4) {
creatjisi(jisi);
}
if(i==5) {
creatwushi(wushi);
}
}
void creatkuangzhanshi(boolean a) {
if(a==true) {
profession = "狂战士";
power = 40+r.nextInt()%5;
speed = 20+r.nextInt()%5;
tili = 30+r.nextInt()%5;
zhili = 5+r.nextInt()%5;
zhihui = 100-power-speed -tili-zhili;
life = tili*20;
magic = (zhili+zhihui)*10;
}
if(a==false) {
System.out.println("您的种族不能选择该职业,请重新选择!");
getprofession();
}
}
void creatshengqishi(boolean a) {
if(a==true) {
profession = "圣骑士";
power = 25+r.nextInt()%5;
speed = 15+r.nextInt()%5;
tili = 30+r.nextInt()%5;
zhili = 20+r.nextInt()%5;
zhihui = 100-power-speed -tili-zhili;
life = tili*20;
magic = (zhili+zhihui)*10;
}
if(a==false) {
System.out.println("您的种族不能选择该职业,请重新选择!");
getprofession();
}
}
void creatcike(boolean a) {
if(a==true) {
profession = "刺客";
power = 20+r.nextInt()%5;
speed = 35+r.nextInt()%5;
tili = 20+r.nextInt()%5;
zhili = 15+r.nextInt()%5;
zhihui = 100-power-speed -tili-zhili;
life = tili*20;
magic = (zhili+zhihui)*10;
}
if(a==false) {
System.out.println("您的种族不能选择该职业,请重新选择!");
getprofession();
}
}
void creatlieshou(boolean a) {
if(a==true) {
profession = "猎手";
power = 15+r.nextInt()%5;
speed = 40+r.nextInt()%5;
tili =15+r.nextInt()%5;
zhili = 10+r.nextInt()%5;
zhihui = 100-power-speed -tili-zhili;
life = tili*20;
magic = (zhili+zhihui)*10;
}
if(a==false) {
System.out.println("您的种族不能选择该职业,请重新选择!");
getprofession();
}
}
void creatjisi(boolean a) {
if(a==true) {
profession = "祭司";
power = 15+r.nextInt()%5;
speed = 20+r.nextInt()%5;
tili = 15+r.nextInt()%5;
zhili = 35+r.nextInt()%5;
zhihui = 100-power-speed -tili-zhili;
life = tili*20;
magic = (zhili+zhihui)*10;
}
if(a==false) {
System.out.println("您的种族不能选择该职业,请重新选择!");
getprofession();
}
}
void creatwushi(boolean a) {
if(a==true) {
profession = "巫师";
power = 10+r.nextInt()%5;
speed = 20+r.nextInt()%5;
tili = 10+r.nextInt()%5;
zhili = 20+r.nextInt()%5;
zhihui = 100-power-speed -tili-zhili;
life = tili*20;
magic = (zhili+zhihui)*10;
}
if(a==false) {
System.out.println("您的种族不能选择该职业,请重新选择!");
getprofession();
}
}
- 将信息保存到文档
public static void writefile() throws Exception{
FileWriter w = new FileWriter("role.txt");
w.write("姓名 "+c.name+"\r\n");
w.write("性别 "+c.sex+"\r\n");
w.write("种族 "+c.race+"\r\n");
w.write("职业 "+c.profession+"\r\n");
w.write("力量 "+c.power+"\r\n");
w.write("敏捷 "+c.speed+"\r\n");
w.write("体力 "+c.tili+"\r\n");
w.write("智力 "+c.zhili+"\r\n");
w.write("智慧 "+c.zhihui+"\r\n");
w.write("生命值 "+c.life+"\r\n");
w.write("魔法值 "+c.magic+"\r\n");
w.close();
}
运行结果