2017-04-02 85 views
0
import java.util.Random; 
public class Enemy extends Character{ 
Random randomNums = new Random(); 

public Enemy (Type enemyType){ 
    super(enemyType); 
} 

public int dropCoins(){ 
    return randomNums.nextInt((50-30)+1)+30; 
} 

public static int getNumGoblins(){ 
    return randomNums.nextInt((5-2)+1)+2; 
} 
public static int getNumSkeletons(){ 
    return randomNums.nextInt((7-3)+1)+3; 
} 

}JAVA:非靜態變量randomNums不能從靜態上下文

import java.util.Random; 
public class Player extends Character{ 
// attributes for the plauer class 
// initilizes the fields 
private int coins; 
private Potion[] inventory; 
Random randomNums = new Random(); 

//methods 

public Player(Type playerType){ 
    super(playerType); 
    coins = 0; 
    inventory = new Potion[5]; 
} 

public void increaseStrength(int strengthIncrease){ 

} 
public int getCoins(){ 
    return coins; 
} 
public void increaseCoins(int coins){ 

} 
public void decreaseCoins(int coins){ 

} 
public void addToInventory(String a, Type potion){ 

} 
public void removeFromInventory(int index){ 

} 
public void displayInventory(){ 

} 
/*public int getNumOpenSlots(){ 
    return numOpenSlots; 
}*/ 

public void battleMinion(Enemy enemy){ 


    Enemy goblin = new Enemy(Character.Type.GOBLIN); 

    if (enemy.getName() == "Goblin"){ 
     for (int i = 1; i <= goblin.getNumGoblins(); i++) 
     { 
      System.out.printf("***%s vs %s %d***\n", getName(), enemy.getName(), i); 

public abstract class Character{ 

public enum Type{ ROGUE, PALADIN, JACKIE_CHEN, SKELETON, GOBLIN, WIZARD} 

private String name; 
private int hitPoints; 
private int strength; 
private Weapon weapon; 
//other attributes 

//methods 

public Character(Type characterType){ 
    switch(characterType){ 
     case ROGUE: 
      //set the attributes for a Rogue 
      name = "Rogue"; 
      // TODO: set other attributes 
      hitPoints = 55; 
      strength = 8; 
      Weapon rogue = new Weapon("Short Sword", 1, 4); 

      break; 
     case PALADIN: 
      //set the attributes for a Rogue 
      name = "Paladin"; 
      // TODO: set other attributes 
      hitPoints = 35; 
      strength = 14; 
      Weapon paladin = new Weapon("Long Sword",3,7); 
      break; 
     case JACKIE_CHEN: 
      name = "Jackie Chen"; 
      hitPoints =45; 
      strength = 10; 
      Weapon jackie = new Weapon("Jump Kick",2, 6); 
      break; 
     case SKELETON: 
      name = "Skeleton"; 
      hitPoints = 25; 
      strength = 3; 
      Weapon skeleton = new Weapon("Short Sword" ,1, 4); 
      break; 
     case GOBLIN: 
      name = "Goblin"; 
      hitPoints = 25; 
      strength = 4; 
      Weapon goblin = new Weapon("Axe",2,6); 
      break; 
     case WIZARD: 
      name = "Wizard"; 
      hitPoints = 40; 
      strength = 8; 
      Weapon wizard = new Weapon("Fire Blast", 4, 10); 
      break; 
    } 
} 

public String getName(){ 
    return name; 
} 

public int getHitPoints(){ 
    return hitPoints; 
} 
public int getStrength(){ 
    return strength; 
} 
public void setStrength(int _strength){ 
    this.strength =_strength; 
} 
public void setWeapon(Weapon _weapon){ 
    this.weapon = _weapon; 

} 

public void attack(){ 

} 

public void increaseHitPoints(){ 

} 
public void decreaseHitPoints(){ 

} 

得到錯誤信息

./Enemy.java:14: error: non-static variable randomNums cannot be referenced from a static context 
     return randomNums.nextInt((5-2)+1)+2; 
      ^
./Enemy.java:17: error: non-static variable randomNums cannot be referenced from a static context 
     return randomNums.nextInt((7-3)+1)+3; 
      ^

2錯誤引用

不知道如何計算它出...

+1

嘗試閱讀我需要使用公共靜態INT錯誤消息 – Justas

回答

0

您正在試圖指靜態方法,這是不允許的內部非靜態成員。

public static int getNumGoblins(){ 
    return randomNums.nextInt((5-2)+1)+2; 
} 
public static int getNumSkeletons(){ 
    return randomNums.nextInt((7-3)+1)+3; 
} 

需要聲明randomNums靜態

static Random randomNums = new Random(); 
+0

感謝您的幫助 – michaeliop

1

因爲randomNums參考是對象範圍的,你不能直接訪問它static方法內側,從而宣告它的範圍爲靜態的,如下所示:

static Random randomNums = new Random(); 

OR無論如何,使用的是調用getNumGoblins()和getNumSkeletons()方法goblin(的Enemy引用類型),所以只從方法簽名除去static關鍵字等:

public int getNumGoblins(){ 
    return randomNums.nextInt((5-2)+1)+2; 
} 
public int getNumSkeletons(){ 
    return randomNums.nextInt((7-3)+1)+3; 
} 
+0

......所以這就是爲什麼我不知道該怎麼辦? – michaeliop

相關問題