2016-09-23 320 views
0

嘗試在java中設計角色扮演遊戲時,代碼在選擇「a」並且名稱輸入爲目標後停止。不知道發生了什麼事。試圖改變而不是一個工作。提前 許多感謝Java角色扮演遊戲

package fantasy.rpg; 
import java.util.Scanner; 
import java.util.ArrayList; 

public class FantasyRPG { 
public static void main(String[] args) { 
    String name; 
    String newName; 
    String race; 

    ArrayList<Creature> Player = new ArrayList(); 

    Scanner reader = new Scanner(System.in); 
    System.out.println("Welcome to the Fantasy Game"+"\n"); 
    System.out.println(" 1. Add the game players"); 
    System.out.println(" 2. Playes play in turn until only one is left"+"\n"); 
    System.out.println("Good Luck!"+"\n"); 
    System.out.println("First, let's add some players:"+"\n"); 
    System.out.println(""); 
    System.out.println("Enter player's name ('quit' when no more players):"); 
    name= reader.nextLine(); 
    System.out.println(name + ", what race are you?"); 
    System.out.println(" H/h: human \n C/c: Cyberdemon \n E/e: Elf \n B/b: Balrog \n"); 
    System.out.print("Please enter your race: "); 
    race = reader.next(); 

    Creature player = new Creature(name, race); 
    Player.add(player); 

    System.out.println(""); 
    System.out.println("Enter player's name ('quit' when no more players):"); 
    newName = reader.next(); 

    while(!"quit".equals(newName)){ 

     System.out.println(newName + ", what race are you?"); 
     System.out.println(" H/h: human \n C/c: Cyberdemon \n E/e: Elf \n B/b: Balrog \n"); 
     System.out.print("Please enter your race: "); 

     race = reader.next(); 
     player = new Creature(newName, race); 
     Player.add(player); 

     System.out.println(""); 
     System.out.println("Enter player's name ('quit' when no more players):"); 
     newName = reader.next(); 
     } 

    System.out.println("Congratulations new fighters. "); 
    System.out.println("You will pitted against each other \nin a fight to the death"); 
    System.out.println("The last one standing wins. \nGood luck."); 
    System.out.println(" |Name| |Species|  |Strength|  |Hit Points|"); 
    for(int count = 0; count <= Player.size()-1; count++){ 
     Creature p = Player.get(count); 
     System.out.println(p.toString());} 
    System.out.println("The Players are ready! \nLet the Battle Begin!"); 
    for(int i=0; i<=Player.size()-1; i++){ 
     Player.get(i); 
     System.out.println(name.toString()+", select one of the following options:"); 
     System.out.println(" a/A: Attack an opponent"); 
     System.out.println(" p/P: Pass (go to the next player"); 
     System.out.println(" q/Q: Quit the game"); 
     String choice=reader.next(); 
      if(choice.equals("a")||choice.equals("A")){ 
       System.out.println("Which player are you attacking? "); 
       String target=reader.next(); 
       int hitp=0; 
       for(int j=0; j<Player.size()-1; j++){ 
        Player.get(j); 
        String targetName=name; 
        if(target.equals(targetName)){ 
         hitp=Creature.getHitpoints(name); 
        } 
        else{ 
        j++; 
       } 
       } 

       int HitDam=Creature.getDamage(race, hitp); 
       Creature.Damage(HitDam); 
      } 
      else if(choice.equals("p")||choice.equals("P")){ 
       return; 
      } 
      else if(choice.equals("q")||choice.equals("Q")){ 
       Player.remove(i); 
      } 
      for(int m=0; m<Player.size()-1; i++){ 
       int stren=0; 
       Player.get(m); 
       stren=Creature.getStrength(); 

       if (Creature.isDead(stren)){ 

        System.out.println("Sorry, "+name.toString()+", but you are dead. Thanks for playing!"); 
          Player.remove(m); 
      } 
      } 
       System.out.println(" |Name| |Species|  |Strength|  |Hit Points|"); 
       for(int count = 0; count <= Player.size()-1; count++){ 
        Creature p = Player.get(count); 
        System.out.println(p.toString()); 
       } 
    } 
    } 
} 

生物類

package fantasy.rpg; 

public class Creature { 
static int Strength; 
static int HitPoints; 
String Name = ""; 
String Species = ""; 
int damage; 

public Creature(String name, String species){ 
    Name = name; 

    if(species.equals("h")||species.equals("H")){ 
     Species = "Human"; 
     Strength = 115; 
     HitPoints = 15;} 
    else if(species.equals("C")||species.equals("c")){ 
     Species = "Cyberdemon"; 
     Strength = 135; 
     HitPoints = 20;} 
    else if(species.equals("E")||species.equals("e")){ 
     Species = "Elf"; 
     Strength = 185; 
     HitPoints = 18; 
     } 
    else if(species.equals("B")||species.equals("b")){ 
     Species = "Balrog"; 
     Strength = 105; 
     HitPoints = 30;} 




    } 
public static int getHitpoints(String player){ 
    return HitPoints; 
} 

public String getSpecies(){ 
    return Species; 
} 
public static int getStrength(){ 

    return Strength; 
} 

public void setStrength(int newStrength){ 
    Strength = newStrength;} 

public void setHitPoints(int newHit){ 
    HitPoints = newHit;} 

public static int getDamage(String Species, int Hit){ 

    int hit = 0+ (int)(Math.random()*(Hit-0)); 
    if(Species == "demon"){ 
     int r =(int)(Math.random()); 
     if(r < .05) 
      hit = hit + 50; 
    } 
    else if(Species == "elf"){ 
     int r = (int)(Math.random()); 
     if(r < .1) 
      hit = hit + 50; 
    } 
    else if(Species == "balrog"){ 
     int balrogDouble = 0 + (int)(Math.random() * Hit - 0); 
     hit = hit + balrogDouble;} 


    int damage = hit; 
    return damage; 
} 

public static void Damage(int damage){ 
    Strength= Strength-damage;} 

public static boolean isDead(int str){ 
    if(str <= 0) 
     return true; 
    else 
     return false; 

     } 

public String getName(){ 
    return Name; 
} 
public boolean isNamed(String aName){ 
    return aName.equals(Name); 
} 

public String toString(){ 
    return " " +Name + "  " + Species + "   " + Strength + "    " + HitPoints; } 



} 
+0

我認爲你需要使用j <= Player.size() - 1。請嘗試一次 –

回答

3

您這裏有一個無限循環

  for(int m=0; m<Player.size()-1; i++){ 

改變我++來M +

您還可以在你的代碼中的另一個問題:

  for(int j=0; j<Player.size()-1; j++){ 
       Player.get(j); 
       String targetName=name; 
       if(target.equals(targetName)){ 
        hitp=Creature.getHitpoints(name); 
       } 
       else{ 
       j++; 
      } 

這裏你得到的是第j個玩家,但是你沒有在任何地方使用它,你只需將targetName分配給當前正在攻擊的玩家名稱。正確的方式來做到這一點是

String targetName = Player.get(j).getName(); 

幾點建議:

  • 重命名播放器播放器,Java約定是使用變量lowerscore名稱,以及多個會記得你是一個列表

  • java有一個內置for循環,這是更直觀的,我會改變上面的for循環是這樣的:

     for(Creature targetPlayer: Player){ 
          String targetName = targetPlayer.getName(); 
          if(target.equals(targetName)){ 
           hitp=targetPlayer.getHitpoints(name);  
          } 
         } 
    
相關問題