Hey,我是寒水,一名大二学生,电子商务专业在读,正在学习Java中。我试图将在Java学习中遇到的一些困惑和最终的解答发在这个账号上,希望以此来激励我不要放弃学习!
import java.util.*;
class Monster{ //定义一个怪物类
String name;
int hp;
int mm;
String skills;
String drop;
}
class monsterHome{
static final int MAXLEN=100; //最多存在100种怪物
Monster[]ListData=new Monster[MAXLEN];
int ListLen; //定义怪物列表元素
void begin(monsterHome world) {
world.ListLen=0;
} // 初始化世界怪物列表为空表
int monsterNumber(monsterHome world) {
return(world.ListLen);
} //返回目前列表中的怪物种类
int monsterBorn(monsterHome world,int n,Monster monster) { //插入怪物数据
if(world.ListLen>=MAXLEN) {
System.out.println("Sorry, the list was full:)");
return 0; //插入失败,返回0值
} //如果特定编号超出了最大值,则插入不成功
if(n<0||n>MAXLEN) {
System.out.println("the number of the monster was wrong!");
return 0;
} //插入怪物的编号不合法,插入失败
for(int i=world.ListLen;i>=n;i--) {
world.ListData[i+1]=world.ListData[i];
}
world.ListData[n]=monster;
world.ListLen++;
return 1;
} //插入怪物数据
int monsterAdd(monsterHome world,Monster monster) { //在列表尾部增加数据
if(world.ListLen>=MAXLEN) {
System.out.println("Sorry,U can not do this way. ");
return 0;
} //超出范围,插入失败
world.ListData[++world.ListLen]=monster;
return 1;
} //在尾部增加数据
int monsterDelete(monsterHome world,int n) { //删除列表元素
if(n<1||n>world.ListLen) {
System.out.println("sorry,you can not do it this way. ");
return 0;
} //超过范围,删除失败,返回0值
for(int i=n;i<world.ListLen;i++) {
world.ListData[i]=world.ListData[i+1];
}
world.ListLen--;
return 1; //删除成功,返回0值
}
Monster monsterFindByNumber(monsterHome world,int n) { //通过序号查找怪物
if(n<1||n>world.ListLen) {
System.out.println("Sorry, you cannot do it this way. ");
return null; //序号部队,查找失败
}
return ListData[n]; //返回怪物属性
}
int monsterFindByName(monsterHome world,String name) { //通过怪物名字查找怪物序号
int i;
for(i=0;i<=world.ListLen;i++) {
if(world.ListData[i].name.compareTo(name)==0){
return i; //查找成功,返回怪物序号
}
}
return 0;
}
int monsterAll(monsterHome world) { //输出怪物列表所有怪物
for(int i=1;i<=world.ListLen;i++) {
System.out.printf("(%s,%d,%d,%s,%s)\n",world.ListData[i].name,world.ListData[i].hp,world.ListData[i].mm,world.ListData[i].skills,world.ListData[i].drop);
}
return 0;
}
}
public class theOrderSheet { //顺序表应用
public static void main(String[] args) {
monsterHome world=new monsterHome();
Monster monster;
String name;
int a;
world.begin(world); //初始化表格
System.out.println(".");
System.out.println("..");
System.out.println("...");
System.out.println("世界初始化完成...");
Scanner input=new Scanner(System.in);
do { //输入怪物
monster=new Monster();
System.out.println("请输入怪物名称,输入“退出输入”以退出输入模式");
monster.name=input.next();
if(monster.name.equals("退出输入")) {
break;
}
System.out.println("请输入怪物血量");
monster.hp=input.nextInt();
System.out.println("请输入怪物魔法值");
monster.mm=input.nextInt();
System.out.println("请输入怪物技能");
monster.skills=input.next();
System.out.println("请输入怪物掉落");
monster.drop=input.next();
if(world.monsterAdd(world, monster)==0) {
break;
}
}while(true);
System.out.println("怪物列表如下:");
world.monsterAll(world); //输出怪物列表
System.out.print("请输入想查看怪物的编号:");
a=input.nextInt();
monster=world.monsterFindByNumber(world, a);
if(monster!=null) {
System.out.printf("第%d个结点的怪物为%s,血量:%d,魔法值:%d,技能:%s,掉落物品:%s\n",a,monster.name,monster.hp,monster.mm,monster.skills,monster.drop);
}
}
}
这是我根据《Java常用算法手册》学习敲出的代码,如有错误还望各位大佬不吝指正!
真的万分感谢,大佬们的指导可以帮助我更快的菜鸟上路呀~