这会从文件中导入文本并创建一个foe类型的对象数组
class cmdquest{
public static void main(String args[]) throws Exception{
//Importing foes.txt to create objects of foes
java.io.File file = new java.io.File("foes.txt");
Scanner imp = new Scanner(file);
foe foes[] = new foe[100];
for(int i =0; i<3; i++){
foes[i]=foe.leDados(imp);
}
}
在另一个班级我有这个,但它不起作用
static void db(){
for(int a=0; a<3; a++){
System.out.print(cmdquest.foes[a].name + "\t");
}
System.out.print("*Press Enter to go back to the main menu*");
Scanner kb = new Scanner(System.in);
kb.nextLine();
Menu.show_menu();
}
这是我的班级敌人,声明了所有内容,包括名称:
class foe{
String name;
int hp;
int str;
int def;
foe(String n, int h, int s, int d) {
name = n;
hp = h;
str = s;
def= d;
}
static foe leDados(Scanner imp){
String foe_name = imp.next();
int foe_hp = imp.nextInt();
int foe_str = imp.nextInt();
int foe_def = imp.nextInt();
return new foe(foe_name, foe_hp, foe_str, foe_def);
}
}
这是我在编译时遇到的错误:
cmdquest.java:186: error: cannot find symbol
System.out.print(cmdquest.foes[a].name + "\t");
^
symbol: variable foes
location: class cmdquest
1 error