java - 难以导航与另一个 2d int 数组相关的 2d String 数组

标签 java arrays multidimensional-array logic

所以我有一个类,其中包含大量有关“游戏玩家”的统计信息。我正在尝试为通过 RNG 实现的某些统计目标实现模拟奖杯系统。 我有一个 2d int 数组,用于保存奖杯的等级/级别的值,一旦实现,奖杯就会从青铜升级到银牌等,以及相应的 2d String保存此类奖杯级别标题的数组。在测试中,似乎适用于现在未使用的方法实际上只为某些类型提供了奖杯。我现在所拥有的是一条我认为我需要遵循的道路才能使其发挥作用。我有一个名为 getBadges(int requestsStat) 的方法,它采用另一个数组的索引值来查看统计奖杯。在该方法中,有一个 for 循环,该循环将方法的参数与两个二维数组进行比较,以确定统计数据的值(存储在另一个数组中)是否有资格获得青铜、银或金奖杯。我的主要问题是我迷失了如何在不超出索引范围的情况下访问二维数组中的这些不同数据点。更不用说当我设置一堆 if-else 语句时,我的测试输出总是生成奖杯的名称,但没有奖杯级别。像这样:

Healer: No Badge 
Explorer: No Badge 
Socialite: No Badge 
Contributor: No Badge 

随着技能点的上升,徽章级别也应该上升(即从“无徽章”到“青铜”等)。这是逻辑或语法错误吗?尽管我做了伪代码的努力,但我对代码中发生的事情感到非常困惑。这是 Gamer 类:

package GamerProject;

import java.io.Serializable;
import java.util.Comparator;

public class Gamer implements Serializable, Comparable<Gamer> {

    private String playerName;
    private static final int HEALTH_POINTS_RESD = 23;
    private static final int AREAS_VISITED = 200;
    private static final int PLAYERS_ENCOUNTERED = 175;
    private static final int MAPS_CREATED = 1500;
    private static final int ITEMS_GATHERED = 20;
    private static final int ITEMS_REPAIRED = 100;
    private static final int ITEMS_MERGED = 125;
    private static final int TOP_SCORES = 250;
    private static final int DMG_POINTS_DEALT = 17;
    private static final int MAPS_COMPLETED = 750;
    private static final int LEVEL2 = 10000;
    private static final int LEVEL3 = 25000;
    private static final int LEVEL4 = 80000;
    private static final int LEVEL5 = 150000;
    private static final int LEVEL6 = 300000;
    private static final int LEVEL7 = 1000000;
    private static final int LEVEL8 = 2200000;
    private static final int LEVEL9 = 4500000;
    private static final int LEVEL10 = 10000000;
    private static final int LEVEL11 = 20000000;
    private static final int LEVEL12 = 35000000;
    private final int[] gamerStatValues = new int[10];
    private final int[] gamerActions = {HEALTH_POINTS_RESD, AREAS_VISITED, PLAYERS_ENCOUNTERED, MAPS_CREATED,
        ITEMS_GATHERED, ITEMS_REPAIRED, ITEMS_MERGED, TOP_SCORES, DMG_POINTS_DEALT, MAPS_COMPLETED};
    private final int[] expValues = {LEVEL2, LEVEL3, LEVEL4, LEVEL5, LEVEL6, LEVEL7,
        LEVEL8, LEVEL9, LEVEL10, LEVEL11, LEVEL12};
    private final int[][] badgePoints = {
        {0, 2000, 10000, 30000, 100000, 200000},
        {0, 50, 1000, 5000, 17000, 40000},
        {0, 100, 1000, 2000, 10000, 30000},
        {0, 3, 10, 20, 90, 150},
        {0, 2000, 10000, 30000, 100000, 200000},
        {0, 100, 1000, 5000, 15000, 40000},
        {0, 100, 500, 2000, 10000, 40000},
        {0, 20, 200, 1000, 5000, 20000},
        {0, 2000, 10000, 30000, 100000, 300000},
        {0, 10, 50, 200, 500, 5000}};
    private final String[] badgeTitles = {"Healer: ", "Explorer: ", "Socialite: ", "Contributor: ",
        "Hoarder: ", "Fixer: ", "Joiner: ", "Leader: ", "Punisher: ", "Obsessed: ",};
    private final String[] badgeRanks = {"No Badge ", "Tin ", "Bronze ", "Silver ", "Gold ", "Platinum "};

    Gamer() {
        playerName = "";

    }

    public int getTotalExp() {
        int totalExp = 0;
        for (int i = 0; i < gamerStatValues.length; i++) {
            totalExp += (gamerStatValues[i] * gamerActions[i]);
        }
        return totalExp;
    }

    public int getLevel() {
        int playerLevel = 1;
        int totalExp = getTotalExp();
        for (int i = 0; i < expValues.length; i++) {
            if (totalExp >= expValues[i]) {
                playerLevel += 1;
                //System.out.println(getTotalExp());
            }
        }
        return playerLevel;
    }

    public String getBadge(int requestedStat) {
        String badgeOutput = "";
        //index = 0;
        if (requestedStat >= 0 && requestedStat <=9) {
            for (int i = 0; i < badgeRanks.length; i++) {//not sure how to get around going out of the array bounds
                if (gamerActions[requestedStat] >= badgePoints[requestedStat][i]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 1]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i];
                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][i+1]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 2]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i+1];
                }
            }
            //did this as an extraneous solution. Still doesn't work
//            if (gamerActions[requestedStat] >= badgePoints[requestedStat][index]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 1]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+1]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 2]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+1];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+2]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 3]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+2];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+3]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 4]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+3];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+4]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 5]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+4];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+5]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 6]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+5];
//                } else {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+6];
//                }
//            

        } else {
            badgeOutput = "No Badges Available";
        }
        return badgeOutput;
    }

    //Incomplete Method
    public String getBadges() {
        String badgeOutput = "Badges: ";
        for (int i = 0; i < badgeTitles.length; i++) {
//            if (gamerActions[i]) {
//                
//            }
        }
        return badgeOutput;
    }

    public String getPlayerName() {
        return playerName;
    }

    public int getHealthPointsResd() {
        return gamerStatValues[0];
    }

    public int getAreasVisited() {
        return gamerStatValues[1];
    }

    public int getPlayersEncountered() {
        return gamerStatValues[2];
    }

    public int getMapsCreated() {
        return gamerStatValues[3];
    }

    public int getItemsGathered() {
        return gamerStatValues[4];
    }

    public int getItemsRepaired() {
        return gamerStatValues[5];
    }

    public int getItemsMerged() {
        return gamerStatValues[6];
    }

    public int getTopScores() {
        return gamerStatValues[7];
    }

    public int getDmgPointsDealt() {
        return gamerStatValues[8];
    }

    public int getMapsCompleted() {
        return gamerStatValues[9];
    }

    //Unused Method
    public void updateRandomGamerAction(int intValue) {
        if (intValue == 0) {
            gamerActions[0]+=1;
        } else if (intValue == 1) {
            gamerActions[1]+=1;
        } else if (intValue == 2) {
            gamerActions[2]+=1;
        } else if (intValue == 3) {
            gamerActions[3]+=1;
        } else if (intValue == 4) {
            gamerActions[4]+=1;
        } else if (intValue == 5) {
            gamerActions[5]+=1;
        } else if (intValue == 6) {
            gamerActions[6]+=1;
        } else if (intValue == 7) {
            gamerActions[7]+=1;
        } else if (intValue == 8) {
            gamerActions[8]+=1;
        } else {
            gamerActions[9]+=1;
        }
    }

    public String setPlayerName(String playerName) {
        this.playerName = playerName;
        return this.playerName;
    }

    public int setHealthPointsResd(int healthPointsResd) {
        if (healthPointsResd >= 0) {
            gamerStatValues[0] = healthPointsResd;
            return gamerStatValues[0];
        } else {
            return gamerStatValues[0];
        }
    }

    public int setAreasVisited(int areasVisited) {
        if (areasVisited >= 0) {
            gamerStatValues[1] = areasVisited;
            return gamerStatValues[1];
        } else {
            return gamerStatValues[1];
        }
    }

    public int setPlayersEncountered(int playersEncountered) {
        if (playersEncountered >= 0) {
            gamerStatValues[2] = playersEncountered;
            return gamerStatValues[2];
        } else {
            return gamerStatValues[2];
        }
    }

    public int setMapsCreated(int mapsCreated) {
        if (mapsCreated >= 0) {
            gamerStatValues[3] = mapsCreated;
            return gamerStatValues[3];
        } else {
            return gamerStatValues[3];
        }
    }

    public int setItemsGathered(int itemsGathered) {
        if (itemsGathered >= 0) {
            gamerStatValues[4] = itemsGathered;
            return gamerStatValues[4];
        } else {
            return gamerStatValues[4];
        }
    }

    public int setItemsRepaired(int itemsRepaired) {
        if (itemsRepaired >= 0) {
            gamerStatValues[5] = itemsRepaired;
            return gamerStatValues[5];
        } else {
            return gamerStatValues[5];
        }
    }

    public int setItemsMerged(int itemsMerged) {
        if (itemsMerged >= 0) {
            gamerStatValues[6] = itemsMerged;
            return gamerStatValues[6];
        } else {
            return gamerStatValues[6];
        }
    }

    public int setTopScores(int topScores) {
        if (topScores >= 0) {
            gamerStatValues[7] = topScores;
            return gamerStatValues[7];
        } else {
            return gamerStatValues[7];
        }
    }

    public int setDmgPointsDealt(int dmgPointsDealt) {
        if (dmgPointsDealt >= 0) {
            gamerStatValues[8] = dmgPointsDealt;
            return gamerStatValues[8];
        } else {
            return gamerStatValues[8];
        }
    }

    public int setMapsCompleted(int mapsCompleted) {
        if (mapsCompleted >= 0) {
            gamerStatValues[9] = mapsCompleted;
            return gamerStatValues[9];
        } else {
            return gamerStatValues[9];
        }
    }

    public void setStatsToZero(){
        for (int i = 0; i < gamerActions.length; i++) {
            gamerActions[i] = 0;
        }
    }
    public String statsString() {
        return "Stats: " + "Health Points Restored = " + gamerStatValues[0]
                + ",\n Areas Visited = " + gamerStatValues[1] + ", PlayersEncountered = " + gamerStatValues[2]
                + ", Maps Created = " + gamerStatValues[3] + ",\n Items Gathered = " + gamerStatValues[4]
                + ", Items Repaired = " + gamerStatValues[5] + ", Items Merged = " + gamerStatValues[6]
                + ",\n Top Scores = " + gamerStatValues[7] + ", Damage Points Dealt =  " + gamerStatValues[8]
                + ", Maps Completed = " + gamerStatValues[9] + '}';
    }

    public String shortDecription() {
        return String.format("%16s: Level %2d, Experience Points: %,10d",
                playerName, this.getLevel(), this.getTotalExp());
    }

    @Override
    public String toString() {
        return "Gamer{" + "Player Name = " + playerName + " Player Stats: "
                + "Health Points Restored = " + gamerStatValues[0]
                + ",\n Areas Visited = " + gamerStatValues[1] + ", PlayersEncountered = " + gamerStatValues[2]
                + ", Maps Created = " + gamerStatValues[3] + ",\n Items Gathered = " + gamerStatValues[4]
                + ", Items Repaired = " + gamerStatValues[5] + ", Items Merged = " + gamerStatValues[6]
                + ",\n Top Scores = " + gamerStatValues[7] + ", Damage Points Dealt =  " + gamerStatValues[8]
                + ", Maps Completed = " + gamerStatValues[9] + '}';
    }

    @Override
    public int compareTo(Gamer player) {
        if (this.getTotalExp() > player.getTotalExp()) {
            return 1;
        } else if (this.getTotalExp() == player.getTotalExp()) {
            return 0;
        } else {
            return -1;
        }
    }

}

这是我正在测试的驱动程序:

package GamerProject;

import java.util.Random;

public class Program7Driver {

    private static final int rngRange = 10;
    private static final Gamer[] gamers = new Gamer[10];
    private static final String[] gamerNames = {"BestGamer99", "CdrShepardN7",
        "Gandalf_The_Cool", "SharpShooter01", "TheDragonborn", "SithLord01",
        "MrWolfenstien", "Goldeneye007", "DungeonMaster91", "MasterThief","TheDarkKnight"};



    public static void main(String[] args) {
        Random rand = new Random();
        for (int i = 0; i < gamers.length; i++) {
            gamers[i] = new Gamer();
            gamers[i].setPlayerName(gamerNames[i]);
            gamers[i].setStatsToZero();

        }
//        for (int i = 0; i < 200000; i++) {
//            int rng = rand.nextInt(rngRange);
//            gamers[rng].setRandomGamerAction(rng);
//        }
        int count = 0;
        for (int i = 0; i < 20000; i++) {
            int rng = rand.nextInt(rngRange);
            System.out.println(gamers[0].getBadge(count));
            //System.out.println(gamers[0].toString());
            //gamers[0].updateRandomGamerAction(rng);
            if (rng == 0) {
                gamers[0].setHealthPointsResd(gamers[0].getHealthPointsResd()+1);
            } else if (rng == 1) {
                gamers[0].setAreasVisited(gamers[0].getAreasVisited()+1);
            } else if (rng == 2) {
                gamers[0].setPlayersEncountered(gamers[0].getPlayersEncountered()+1);
            } else if (rng == 3) {
                gamers[0].setMapsCreated(gamers[0].getMapsCreated()+1);
            } else if (rng == 4) {
                gamers[0].setItemsGathered(gamers[0].getItemsGathered()+1);
            } else if (rng == 5) {
                gamers[0].setItemsRepaired(gamers[0].getItemsRepaired()+1);
            } else if (rng == 6) {
                gamers[0].setItemsMerged(gamers[0].getItemsMerged()+1);
            } else if (rng == 7) {
                gamers[0].setTopScores(gamers[0].getTopScores()+1);
            } else if (rng == 8) {
                gamers[0].setDmgPointsDealt(gamers[0].getDmgPointsDealt()+1);
            } else  {
                gamers[0].setMapsCompleted(gamers[0].getMapsCompleted()+1);
            }

            count += 1;
            if (count == 10) {
                count -= 10;
            }
           // System.out.println(gamers[i].statsString());     
        }
    }
}

最佳答案

好的,我做了一些改变。看看这是否符合您的要求:

package GamerProject;

import java.io.Serializable;
import java.util.Comparator;

public class Gamer implements Serializable, Comparable<Gamer> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String playerName;
    private static final int HEALTH_POINTS_RESD = 23;
    private static final int AREAS_VISITED = 200;
    private static final int PLAYERS_ENCOUNTERED = 175;
    private static final int MAPS_CREATED = 1500;
    private static final int ITEMS_GATHERED = 20;
    private static final int ITEMS_REPAIRED = 100;
    private static final int ITEMS_MERGED = 125;
    private static final int TOP_SCORES = 250;
    private static final int DMG_POINTS_DEALT = 17;
    private static final int MAPS_COMPLETED = 750;
    private static final int LEVEL2 = 10000;
    private static final int LEVEL3 = 25000;
    private static final int LEVEL4 = 80000;
    private static final int LEVEL5 = 150000;
    private static final int LEVEL6 = 300000;
    private static final int LEVEL7 = 1000000;
    private static final int LEVEL8 = 2200000;
    private static final int LEVEL9 = 4500000;
    private static final int LEVEL10 = 10000000;
    private static final int LEVEL11 = 20000000;
    private static final int LEVEL12 = 35000000;
    private final int[] gamerStatValues = new int[10];
    private final int[] gamerActions = {HEALTH_POINTS_RESD, AREAS_VISITED, PLAYERS_ENCOUNTERED, MAPS_CREATED,
        ITEMS_GATHERED, ITEMS_REPAIRED, ITEMS_MERGED, TOP_SCORES, DMG_POINTS_DEALT, MAPS_COMPLETED};
    private final int[] expValues = {LEVEL2, LEVEL3, LEVEL4, LEVEL5, LEVEL6, LEVEL7,
        LEVEL8, LEVEL9, LEVEL10, LEVEL11, LEVEL12};
    private final int[][] badgePoints = {
        {0, 2000, 10000, 30000, 100000, 200000},
        {0, 50, 1000, 5000, 17000, 40000},
        {0, 100, 1000, 2000, 10000, 30000},
        {0, 3, 10, 20, 90, 150},
        {0, 2000, 10000, 30000, 100000, 200000},
        {0, 100, 1000, 5000, 15000, 40000},
        {0, 100, 500, 2000, 10000, 40000},
        {0, 20, 200, 1000, 5000, 20000},
        {0, 2000, 10000, 30000, 100000, 300000},
        {0, 10, 50, 200, 500, 5000}};
    private final String[] badgeTitles = {"Healer: ", "Explorer: ", "Socialite: ", "Contributor: ",
        "Hoarder: ", "Fixer: ", "Joiner: ", "Leader: ", "Punisher: ", "Obsessed: ",};
    private final String[] badgeRanks = {"No Badge ", "Tin ", "Bronze ", "Silver ", "Gold ", "Platinum "};

    Gamer() {
        playerName = "";

    }

    public int getTotalExp() {
        int totalExp = 0;
        for (int i = 0; i < gamerStatValues.length; i++) {
            totalExp += (gamerStatValues[i] * gamerActions[i]);
        }
        return totalExp;
    }

    public int getLevel() {
        int playerLevel = 1;
        int totalExp = getTotalExp();
        for (int i = 0; i < expValues.length; i++) {
            if (totalExp >= expValues[i]) {
                playerLevel += 1;
                //System.out.println(getTotalExp());
            }
        }
        return playerLevel;
    }

    public String getBadge(int requestedStat) {
        String badgeOutput = "";
        //index = 0;
        if (requestedStat >= 0 && requestedStat <=9) {
            for (int i = 0; i < badgeRanks.length; i++) {//not sure how to get around going out of the array bounds
                if (gamerActions[requestedStat] >= badgePoints[requestedStat][i]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 1]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i];
                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][i+1]
                        && gamerActions[requestedStat] < badgePoints[requestedStat][i + 2]) {
                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[i+1];
                }
            }
            //did this as an extraneous solution. Still doesn't work
//            if (gamerActions[requestedStat] >= badgePoints[requestedStat][index]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 1]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+1]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 2]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+1];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+2]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 3]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+2];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+3]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 4]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+3];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+4]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 5]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+4];
//                } else if (gamerActions[requestedStat] >= badgePoints[requestedStat][index+5]
//                        && gamerActions[requestedStat] < badgePoints[requestedStat][index + 6]) {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+5];
//                } else {
//                    badgeOutput = badgeTitles[requestedStat] + badgeRanks[index+6];
//                }
//            

        } else {
            badgeOutput = "No Badges Available";
        }
        return badgeOutput;
    }

    //Incomplete Method
    public String getBadges() {
        String badgeOutput = "Badges: ";
        for (int i = 0; i < badgeTitles.length; i++) {
//            if (gamerActions[i]) {
//                
//            }
        }

        return badgeOutput;
    }

    public String getPlayerName() {
        return playerName;
    }

    public int getHealthPointsResd() {
        return gamerStatValues[0];
    }

    public int getAreasVisited() {
        return gamerStatValues[1];
    }

    public int getPlayersEncountered() {
        return gamerStatValues[2];
    }

    public int getMapsCreated() {
        return gamerStatValues[3];
    }

    public int getItemsGathered() {
        return gamerStatValues[4];
    }

    public int getItemsRepaired() {
        return gamerStatValues[5];
    }

    public int getItemsMerged() {
        return gamerStatValues[6];
    }

    public int getTopScores() {
        return gamerStatValues[7];
    }

    public int getDmgPointsDealt() {
        return gamerStatValues[8];
    }

    public int getMapsCompleted() {
        return gamerStatValues[9];
    }

    //Unused Method
    public void updateRandomGamerAction(int intValue) {
        if (intValue == 0) {
            gamerActions[0]+=1;
        } else if (intValue == 1) {
            gamerActions[1]+=1;
        } else if (intValue == 2) {
            gamerActions[2]+=1;
        } else if (intValue == 3) {
            gamerActions[3]+=1;
        } else if (intValue == 4) {
            gamerActions[4]+=1;
        } else if (intValue == 5) {
            gamerActions[5]+=1;
        } else if (intValue == 6) {
            gamerActions[6]+=1;
        } else if (intValue == 7) {
            gamerActions[7]+=1;
        } else if (intValue == 8) {
            gamerActions[8]+=1;
        } else {
            gamerActions[9]+=1;
        }
    }

    public String setPlayerName(String playerName) {
        this.playerName = playerName;
        return this.playerName;
    }

    public int setHealthPointsResd(int healthPointsResd) {
        if (healthPointsResd >= 0) {
            gamerStatValues[0] = healthPointsResd;
            return gamerStatValues[0];
        } else {
            return gamerStatValues[0];
        }
    }

    public int setAreasVisited(int areasVisited) {
        if (areasVisited >= 0) {
            gamerStatValues[1] = areasVisited;
            return gamerStatValues[1];
        } else {
            return gamerStatValues[1];
        }
    }

    public int setPlayersEncountered(int playersEncountered) {
        if (playersEncountered >= 0) {
            gamerStatValues[2] = playersEncountered;
            return gamerStatValues[2];
        } else {
            return gamerStatValues[2];
        }
    }

    public int setMapsCreated(int mapsCreated) {
        if (mapsCreated >= 0) {
            gamerStatValues[3] = mapsCreated;
            return gamerStatValues[3];
        } else {
            return gamerStatValues[3];
        }
    }

    public int setItemsGathered(int itemsGathered) {
        if (itemsGathered >= 0) {
            gamerStatValues[4] = itemsGathered;
            return gamerStatValues[4];
        } else {
            return gamerStatValues[4];
        }
    }

    public int setItemsRepaired(int itemsRepaired) {
        if (itemsRepaired >= 0) {
            gamerStatValues[5] = itemsRepaired;
            return gamerStatValues[5];
        } else {
            return gamerStatValues[5];
        }
    }

    public int setItemsMerged(int itemsMerged) {
        if (itemsMerged >= 0) {
            gamerStatValues[6] = itemsMerged;
            return gamerStatValues[6];
        } else {
            return gamerStatValues[6];
        }
    }

    public int setTopScores(int topScores) {
        if (topScores >= 0) {
            gamerStatValues[7] = topScores;
            return gamerStatValues[7];
        } else {
            return gamerStatValues[7];
        }
    }

    public int setDmgPointsDealt(int dmgPointsDealt) {
        if (dmgPointsDealt >= 0) {
            gamerStatValues[8] = dmgPointsDealt;
            return gamerStatValues[8];
        } else {
            return gamerStatValues[8];
        }
    }

    public int setMapsCompleted(int mapsCompleted) {
        if (mapsCompleted >= 0) {
            gamerStatValues[9] = mapsCompleted;
            return gamerStatValues[9];
        } else {
            return gamerStatValues[9];
        }
    }

    public void setStatsToZero(){
        for (int i = 0; i < gamerActions.length; i++) {
            gamerActions[i] = 0;
        }
    }
    public String statsString() {
        return "Stats: " + "Health Points Restored = " + gamerStatValues[0]
                + ",\n Areas Visited = " + gamerStatValues[1] + ", PlayersEncountered = " + gamerStatValues[2]
                + ", Maps Created = " + gamerStatValues[3] + ",\n Items Gathered = " + gamerStatValues[4]
                + ", Items Repaired = " + gamerStatValues[5] + ", Items Merged = " + gamerStatValues[6]
                + ",\n Top Scores = " + gamerStatValues[7] + ", Damage Points Dealt =  " + gamerStatValues[8]
                + ", Maps Completed = " + gamerStatValues[9] + '}';
    }

    public String shortDecription() {
        return String.format("%16s: Level %2d, Experience Points: %,10d",
                playerName, this.getLevel(), this.getTotalExp());
    }

    @Override
    public String toString() {
        return "Gamer{" + "Player Name = " + playerName + " Player Stats: "
                + "Health Points Restored = " + gamerStatValues[0]
                + ",\n Areas Visited = " + gamerStatValues[1] + ", PlayersEncountered = " + gamerStatValues[2]
                + ", Maps Created = " + gamerStatValues[3] + ",\n Items Gathered = " + gamerStatValues[4]
                + ", Items Repaired = " + gamerStatValues[5] + ", Items Merged = " + gamerStatValues[6]
                + ",\n Top Scores = " + gamerStatValues[7] + ", Damage Points Dealt =  " + gamerStatValues[8]
                + ", Maps Completed = " + gamerStatValues[9] + '}';
    }

    @Override
    public int compareTo(Gamer player) {
        if (this.getTotalExp() > player.getTotalExp()) {
            return 1;
        } else if (this.getTotalExp() == player.getTotalExp()) {
            return 0;
        } else {
            return -1;
        }
    }

}

另一个:

package GamerProject;

import java.util.Random;

public class Program7Driver {

    private static final int rngRange = 10;
    private static final Gamer[] gamers = new Gamer[10];
    private static final String[] gamerNames = {"BestGamer99", "CdrShepardN7",
        "Gandalf_The_Cool", "SharpShooter01", "TheDragonborn", "SithLord01",
        "MrWolfenstien", "Goldeneye007", "DungeonMaster91", "MasterThief","TheDarkKnight"};



    public static void main(String[] args) {
        Random rand = new Random();
        for (int i = 0; i < gamers.length; i++) {
            gamers[i] = new Gamer();
            gamers[i].setPlayerName(gamerNames[i]);
            gamers[i].setStatsToZero();
        }
//        for (int i = 0; i < 200000; i++) {
//            int rng = rand.nextInt(rngRange);
//            gamers[rng].setRandomGamerAction(rng);
//        }
        int count = 0;
        for (int i = 0; i < gamers.length; i++) {
            int rng = rand.nextInt(rngRange);
            System.out.println(gamers[i]);
            //System.out.println(gamers[0].toString());
            //gamers[0].updateRandomGamerAction(rng);
            if (rng == 0) {
                gamers[0].setHealthPointsResd(gamers[0].getHealthPointsResd()+1);
            } else if (rng == 1) {
                gamers[0].setAreasVisited(gamers[0].getAreasVisited()+1);
            } else if (rng == 2) {
                gamers[0].setPlayersEncountered(gamers[0].getPlayersEncountered()+1);
            } else if (rng == 3) {
                gamers[0].setMapsCreated(gamers[0].getMapsCreated()+1);
            } else if (rng == 4) {
                gamers[0].setItemsGathered(gamers[0].getItemsGathered()+1);
            } else if (rng == 5) {
                gamers[0].setItemsRepaired(gamers[0].getItemsRepaired()+1);
            } else if (rng == 6) {
                gamers[0].setItemsMerged(gamers[0].getItemsMerged()+1);
            } else if (rng == 7) {
                gamers[0].setTopScores(gamers[0].getTopScores()+1);
            } else if (rng == 8) {
                gamers[0].setDmgPointsDealt(gamers[0].getDmgPointsDealt()+1);
            } else  {
                gamers[0].setMapsCompleted(gamers[0].getMapsCompleted()+1);
            }

            count += 1;
            if (count == 10) {
                count -= 10;
            }
           // System.out.println(gamers[i].statsString());     
        }
    }
}

关于java - 难以导航与另一个 2d int 数组相关的 2d String 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27285756/

相关文章:

java - Spring - "late binding" Autowiring 的 bean

c - 取消引用结构体中的数组(在 C 中)

c - 在 C89 中为二维数组分配内存以使用函数打印矩阵

c++ - 将每行中具有不同单词数的文本文件读入C++中的二维数组

ios - NSMutableArray 多维覆盖自身

java - 如何在 Spring Integration 中从 TCP 读取流(消息)

java - 在文件中存储具有相同键的多个数据并从 shell 脚本获取值?

java - 重构重写的方法,它们只在主体的中间有区别

javascript - 如何显示数组中的数组项

c - 我的数组 printf 循环在末尾缺少一位数字