1.分两个包
package com.momo.domain;
public class People {
String name;
String gender;
public People() {
}
public People(String name, String gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(int x) {
switch(x){
case 0:
gender="男性";
break;
case 1:
gender="女性";
break;
default:
break;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
People people = (People) o;
if (!name.equals(people.name)) return false;
return gender.equals(people.gender);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + gender.hashCode();
return result;
}
}
2.package com.momo.domain;
import java.util.Scanner;
public class Race extends People{
protected String race;
protected String oppucation;
public Race() {
}
public Race(String name, String gender, String race, String oppucation) {
super(name, gender);
this.race = race;
this.oppucation = oppucation;
}
public String getRace() {
return race;
}
public String getOppucation() {
return oppucation;
}
public void setRace(int x) {
switch (x){
case 0:
race="人类";
break;
case 1:
race="精灵";
break;
case 2:
race="兽人";
break;
case 3:
race="矮人";
break;
case 4:
race="元素";
break;
default:
break;
}
}
public void setOppucation() {
Scanner sc = new Scanner(System.in);
if(race.equals("人类")){
System.out.println("0狂战士,1圣骑士,2刺客,3猎手,4祭司,5巫师");
int x = sc.nextInt();
switch (x){
case 0:
oppucation="狂战士";
break;
case 1:
oppucation="圣骑士";
break;
case 2:
oppucation="刺客";
break;
case 3:
oppucation="猎手";
break;
case 4:
oppucation="祭司";
break;
case 5:
oppucation="巫师";
break;
default:
break;
}
}else if(race.equals("精灵")){
System.out.println("0狂战士(不允许),1圣骑士(不允许),2刺客,3猎手,4祭司,5巫师");
int x = sc.nextInt();
switch (x){
case 2:
oppucation="刺客";
break;
case 3:
oppucation="猎手";
break;
case 4:
oppucation="祭司";
break;
case 5:
oppucation="巫师";
break;
default:
System.out.println("没有该职业或不允许选择");
break;
}
}else if(race.equals("兽人")){
System.out.println("0狂战士,1圣骑士(不允许),2刺客(不允许),3猎手,4祭司,5巫师(不允许)");
int x = sc.nextInt();
switch (x){
case 0:
oppucation="狂战士";
break;
case 3:
oppucation="猎手";
break;
case 4:
oppucation="祭司";
break;
default:
System.out.println("没有该职业或不允许选择");
break;
}
}else if(race.equals("矮人")){
System.out.println("0狂战士,1圣骑士,2刺客(不允许),3猎手(不允许),4祭司,5巫师(不允许)");
int x = sc.nextInt();
switch (x){
case 0:
oppucation="狂战士";
break;
case 1:
oppucation="圣骑士";
break;
case 4:
oppucation="祭司";
break;
default:
System.out.println("没有该职业或不允许选择");
break;
}
}else if(race.equals("元素")){
System.out.println("0狂战士(不允许),1圣骑士(不允许),2刺客(不允许),3猎手(不允许),4祭司,5巫师");
int x = sc.nextInt();
switch (x){
case 4:
oppucation="祭司";
break;
case 5:
oppucation="巫师";
break;
default:
System.out.println("没有该职业或不允许选择");
break;
}
}
}
}
3.package com.momo.domain;
import java.util.Arrays;
import java.util.Random;
public class Property extends Race {
int ll;
int zl;
int zh;
int mj;
int tl;
int mp;
int hp;
public Property() {
}
public Property(String name, String gender, String race, String oppucation, int ll, int zl, int zh, int mj, int mp, int hp) {
super(name, gender, race, oppucation);
this.ll = ll;
this.zl = zl;
this.zh = zh;
this.mj = mj;
this.mp = mp;
this.hp = hp;
}
public Property(String name, String gender, String race, String oppucation, int ll, int zl, int zh, int mj, int tl, int mp, int hp) {
super(name, gender, race, oppucation);
this.ll = ll;
this.zl = zl;
this.zh = zh;
this.mj = mj;
this.tl = tl;
this.mp = mp;
this.hp = hp;
}
public int[] setZhi(){
Random rand = new Random();
int[] arr = new int[5];
while (true) {
int sum = 0;
for (int i = 0; i < 5; i++) {
arr[i] = rand.nextInt(36) + 5;
sum += arr[i];
}
if (sum == 100) {
break;
}
}
Arrays.sort(arr);
return arr;
}
public void setSx() {
int[] arr = setZhi();
if (oppucation.equals("狂战士")) {
ll = arr[4];
tl = arr[3];
mj = arr[2];
zl = arr[1];
zh = arr[0];
} else if (oppucation.equals("圣骑士")) {
tl = arr[4];
ll = arr[3];
zl = arr[2];
mj = arr[1];
zh = arr[0];
} else if (oppucation.equals("刺客")) {
mj = arr[4];
tl = arr[3];
ll = arr[2];
zl = arr[1];
zh = arr[0];
} else if (oppucation.equals("猎手")) {
mj = arr[4];
zh = arr[3];
tl = arr[2];
ll = arr[1];
zl = arr[0];
} else if (oppucation.equals("祭司")) {
zl = arr[4];
mj = arr[3];
zh = arr[2];
tl = arr[1];
ll = arr[0];
} else if (oppucation.equals("巫师")) {
zh = arr[4];
zl = arr[3];
mj = arr[2];
tl = arr[1];
ll = arr[0];
}
hp = tl * 20;
mp = (zl + zh) * 10;
}
@Override
public String toString() {
return "Property{" +
"ll=" + ll +
", zl=" + zl +
", zh=" + zh +
", mj=" + mj +
", tl=" + tl +
", mp=" + mp +
", hp=" + hp +
", race='" + race + '\'' +
", oppucation='" + oppucation + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
4.test
package com.momo.test;
import com.momo.domain.Property;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Demo {
public static void main(String[]args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
System.out.println("请输入名字:");
Scanner sc = new Scanner(System.in);
Property p = new Property();
String name=sc.next();
System.out.println("请输入性别:0男性,1女性");
int x = sc.nextInt();
p.setGender(x);
System.out.println("请选择种族:0人类,1精灵,2兽人,3矮人,4元素");
int x2 = sc.nextInt();
p.setRace(x2);
p.setOppucation();
p.setSx();
System.out.println(p.toString());
bw.write(p.toString());
bw.close();
}
}
```欢迎指点