import java.util.*;
import java.io.*;
public class LootGenerator{
public static void main (String[] args)
throws FileNotFoundException{
System.out.println("This program simulates the random item generator");
System.out.println("from the game Diablo II. Happy hunting!");
System.out.println();
String file="monstats.txt";
Monster[] array = getArray(file);
//Monster alex=getRandomMonster(array);
}
public Monster getRandomMonster(Monster[] array){
int randMonster = (int)(Math.random() * array.length);
return array[randMonster];
}
public Monster[] getArray (String file)
throws FileNotFoundException{
Scanner sc = new Scanner (new File (file));
Monster[] array = new Monster[sc.nextInt()];
sc.next(); //takes away the word Class
sc.next(); //takes away the word Type
sc.next(); //takes away the word Level
sc.next(); //takes away the word TreasureClass
for(int a = 0; a < array.length; a++)
{
array[a] = new Monster(sc.next(), sc.next(), sc.nextInt(), sc.next());
}
return array;
}
}
class Monster{
private String monsterClass;
private String type;
private int level;
private String treasureClass;
public Monster(String myClass, String myType, int myLevel, String myTreasureClass){
monsterClass = myClass;
type = myType;
level = myLevel;
treasureClass = myTreasureClass;
}
public String getMonsterClass(){
return monsterClass;
}
public String getType(){
return type;
}
public int getLevel(){
return level;
}
public String getTreasureClass(){
return treasureClass;
}
我想不出有什麼不對我的計劃......有什麼建議?我一直得到它不能從靜態上下文中引用 - 行Monster [] array = getArray(file)就是。該計劃的目的是隨機生成一個文本文件中的怪物 - 轉讓是在這裏,如果你需要看文本文件本身:http://www.cis.upenn.edu/~cis110/hw/hw06/index.html
您不能引用從靜態方法實例方法,......是的getArray未聲明爲static –