作业说明:
完一个PRG角色生成器,主要创建以下几个类:Base()基类,Race()继承Base()类,Attribut()类继承Race()类,reality()实现主要功能。生成的数据结果保存在write.txt文档中。
Base 类:
import java.util.Scanner;
public class Base {
protected String name;
protected String sex;
int sex_choice;
public void getBace() {
int i = 0;
System.out.println(" 输入角色名字:");
Scanner scanner = new Scanner(System.in);
name = scanner.next();
System.out.println("选择角色性别:");
System.out.println("1男 2女");
Scanner s3 = new Scanner(System.in);
i = s3.nextInt();
if (i == 1) {
sex = "男";
} else if (i == 2) {
sex = "女";
} else {
System.out.println("输入错误,请重新输入");
}
}
}
Race类:
import java.util.Scanner;
public class Race extends Base {
protected String race;
protected String occupation;
protected int race_choice;
protected int occupation_choice;
// 选择种族和职员
public void get_Race() {
boolean l = true;
if (l) {
System.out.println("请选择种族: \n");
System.out.println("1人类 2精灵 3兽人 4矮人 5元素");
Scanner scanner = new Scanner(System.in);
race_choice = scanner.nextInt();
int i;
switch (race_choice) {
case 1:
race = "精灵";
i = 0;
break;
case 2:
race = "精灵";
i = 0;
break;
case 3:
race = "兽人";
i = 0;
break;
case 4:
race = "矮人";
i = 0;
break;
case 5:
race = "元素";
i = 0;
break;
default:
System.out.println("输入错误,请重新输入! \n");
break;
}
}
boolean m = true;
while (m) {
System.out.println("可以使用的职业: \n");
switch (race_choice) {
case 1:
System.out.println("1.狂战士 2.圣骑士 3.刺客 4.猎手 5.祭司 6.巫师\n");
break;
case 2:
System.out.println("3.刺客 4.猎手 5.祭司 6.巫师\n");
break;
case 3:
System.out.println("1.狂战士 4.猎手 5.祭司 \n");
break;
case 4:
System.out.println("1.狂战士 2.圣骑士 5.祭司\n");
break;
case 5:
System.out.println("5.祭司 6.巫师\n");
break;
}
Scanner s = new Scanner(System.in);
occupation_choice = s.nextInt();
if (race_choice == 1 && (occupation_choice >= 1 && occupation_choice <= 6))
break;
else if (race_choice == 2 && (occupation_choice >= 3 && occupation_choice <= 6))
break;
else if (race_choice == 3 && (occupation_choice == 1 || occupation_choice == 4 || occupation_choice == 5))
break;
else if (race_choice == 4 && (occupation_choice == 1 || occupation_choice == 2 || occupation_choice == 5))
break;
else if (race_choice == 5 && (occupation_choice >= 5 && occupation_choice <= 6))
break;
else
System.out.println("输入错误,请重新输入");
while (occupation_choice == 1) {
occupation = "狂战士";
System.out.println(occupation + "狂战士");
}
while (occupation_choice == 2) {
occupation = "圣骑士";
System.out.println(occupation + "圣骑士");
}
while (occupation_choice == 3) {
occupation = "刺客";
System.out.println(occupation + "刺客");
}
while (occupation_choice == 4) {
occupation = "猎手";
System.out.println(occupation + "猎手");
}
while (occupation_choice == 5) {
occupation = "祭司";
System.out.println(occupation + "祭司");
}
while (occupation_choice == 6) {
occupation = "巫师";
System.out.println(occupation + "巫师");
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Attribute类:
//记录角色的属性
public class Attribute extends Race {
protected int strength; // 力量
protected int agility; // 敏捷
protected int physical; // 体力
protected int intelligence; // 智力
protected int wisdom; // 智慧
protected int HP; // 血量
protected int MP; // 法力值
protected Race r1 = new Race();
// 根据选择的职业,向getRamdom传各职业最小值
public void getAttribute() {
// System.out.println("输入职业:");
// Scanner s1 = new Scanner(System.in);
// int occupation_choice = s1.nextInt();
if (r1.occupation_choice == 1)
getRandom(40, 20, 30, 5, 5);// 狂战士
if (r1.occupation_choice == 2)
getRandom(25, 15, 30, 20, 10);// 圣骑士
if (r1.occupation_choice == 3)
getRandom(20, 35, 20, 15, 10);// 刺客
if (r1.occupation_choice == 4)
getRandom(15, 40, 15, 10, 20);// 猎手
if (r1.occupation_choice == 5)
getRandom(15, 20, 15, 35, 15);// 祭司
if (r1.occupation_choice == 6)
getRandom(10, 20, 10, 20, 40);// 巫师
}
// 随机生成每项属性的值,abcd为该属性的最小值,e为第五个属性的最大值
public void getRandom(int a, int b, int c, int d, int e) {
int sum; // 前4项属性之和
do {
strength = (int) (a + Math.random() % 3);
agility = (int) (b + Math.random() % 3);
physical = (int) (c + Math.random() % 3);
intelligence = (int) (d + Math.random() % 3);
sum = strength + agility + physical + intelligence;
} while (((100 - e) < sum) && (sum < 100));
wisdom = 100 - sum;
HP = physical * 20;
MP = (wisdom + intelligence) * 10;
System.out.println("力量:" + strength);
System.out.println("敏捷:" + agility);
System.out.println("体力:" + physical);
System.out.println("智力:" + intelligence);
System.out.println("总值:" + sum);
System.out.println("智慧:" + wisdom);
System.out.println("血量:" + HP);
System.out.println("法力值:" + MP);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
reality 类:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class reality {// 文件输出类,将用户的id和得分在、输出到文件中
protected static Attribute attribute = new Attribute();
public static void main(String args[]) {
new reality();
int player_choice;
do {
attribute.getBace();
attribute.get_Race();
attribute.getRandom(10, 20, 23, 12, 10);
System.out.println("对生成的角色是否满意?如不满意,则重新生成\n");
System.out.println("0.满意 1.不满意\n");
Scanner s2 = new Scanner(System.in);
player_choice = s2.nextInt();
break;
} while (true);
OutTxt outtxt = new OutTxt();
outtxt.outtxt(attribute);
}
}
class OutTxt {
public void outtxt(Attribute a) {// 文件输出方法
try {
BufferedWriter outsco = new BufferedWriter(new FileWriter(new File("write.txt")));// 文件输出流
String text;
text = "name=" + a.name + " " + "sex=" + a.sex + " " + "race=" + a.race + " " + "occupation="
+ a.occupation + " " + "strength=" + a.strength + " " + "agility=" + a.agility + " " + "wisdom="
+ a.wisdom + " " + "intelligence=" + a.intelligence + " " + "HP=" + a.HP + " " + "MP=" + a.MP;
outsco.write(text);// 调用writer方法,将用户信息写入文件中
outsco.flush();// 刷新输出流
outsco.close();// 关闭流
} catch (Exception e) {
System.out.println("文件输出错误");
}
}
}