首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java角色扮演游戏

Java角色扮演游戏
EN

Stack Overflow用户
提问于 2016-09-23 02:55:38
回答 1查看 1.6K关注 0票数 0

试图在java中设计一个角色扮演游戏,在选择"a“并输入名称作为目标后,代码停止。不知道是怎么回事。试着改一个for,但是没有什么效果。事先非常感谢

代码语言:javascript
复制
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());
                }
    }
    }
}

生物类

代码语言:javascript
复制
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; }



}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-23 03:28:16

这里有一个无限的循环

代码语言:javascript
复制
            for(int m=0; m<Player.size()-1; i++){

将i++更改为m++

您的代码中还有另一个问题:

代码语言:javascript
复制
            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分配给正在攻击的当前玩家名。正确的方法是

代码语言:javascript
复制
String targetName = Player.get(j).getName();

一些建议:

  • 将Player重命名为Player,java约定使用低分数名作为变量,并且复数将记住您是一个列表。
  • java有一个内置的for循环,它更直观,我会将上面的for循环更改为如下所示: for(生物targetPlayer: Player){ String targetName = targetPlayer.getName();if(target.equals(targetName)){ hitp=targetPlayer.getHitpoints(名称);}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39651854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档