游戏得分简单算法

时间:2014-10-02 09:31:46

标签: java algorithm

取决于我的游戏完成百分比水平我将显示一些消息,我编写了这个伪代码,它可能不起作用只是为了让你明白这一点。

" pro"做以下的方式...

if (successPercent == 100)
        successMessage = "OUTSTANDING";
    else if (successPercent < 100 && successPercent > 80)
        successMessage = "EXCELENT";
    else if (successPercent < 100 && successPercent > 60)
        successMessage = "VERY GOOD";
    else if (successPercent < 100 && successPercent > 40)
        successMessage = "GOOD";
    else if (successPercent < 100 && successPercent > 10)
        successMessage = "AVERAGE";
    else if (successPercent > 0 && successPercent < 10)
        successMessage = "BAD";
    else if (successPercent == 0)
        successMessage = "TERRIBLE";

4 个答案:

答案 0 :(得分:3)

每次都不需要"successPercent < 100"部分,因为如果当前的if不正确,你只能到达下一个部分。

   if (successPercent == 100)
        successMessage = "OUTSTANDING";
    else if (successPercent > 80)
        successMessage = "EXCELENT";
    else if (successPercent > 60)
        successMessage = "VERY GOOD";
    else if (successPercent > 40)
        successMessage = "GOOD";
    else if (successPercent > 10)
        successMessage = "AVERAGE";
    else if (successPercent!=0 && successPercent < 10)
        successMessage = "BAD";
    else if (successPercent == 0)
        successMessage = "TERRIBLE";

答案 1 :(得分:2)

我会创建一个包含所有可能分数的枚举。然后,您可以调用静态方法getGameScore,该方法将遍历所有游戏分数,直到找到与给定游戏核心相匹配的方法。这样可以避免大量重复的代码以及所有其他代码,如果这些代码也不容易读取代码。

根据你想要对不在正确范围内的分数做什么,你需要做一些比我写的更多的东西,目前如果输入-1或101的分数,它会返回空引用。

/**
 * Enum representing all possible game scores.
 * Each game score has a lower limit and upper limit.
 */
public enum GameScore {
  OUTSTANCING(100, 100),
  EXCELLENT(80, 99),
  VERY_GOOD(60, 79),
  GOOD(40, 59),
  AVERAGE(10, 39),
  BAD(1, 9),
  TERRIBLE(0, 0)
  ;

  // The max of the range for this GameScore
  private final Integer min;
  // The min of the range for this GameScore
  private final Integer max;

  /**
   * Constructs a gameScore type by specifying the range.
   * @param min
   * The minimum of the range.
   * @param max
   * The maximum of the range.
   */
  private GameScore(final Integer min, final Integer max) {
    this.min = min;
    this.max = max;
  }

  /**
   * Searches a game score based on a given score.
   * @param score
   * The score for which a GameScore is searched for.
   * @return
   * Returns the first GameScore that matches the given score. This means that
   * the given score falls in the range of the GameScore.
   * Will return null if no matching GameScore is found.
   */
  public static GameScore getGameScore(final Integer score) {
    for(GameScore gameScore : values()) {
      if(score >= min && score <= max) {
        return gameScore;
      }
    }
    return null;
  }

答案 2 :(得分:1)

你可以试试这种方式

    if (successPercent == 100)
        successMessage = "OUTSTANDING";
    else if (successPercent > 80)
        successMessage = "EXCELENT";
    else if (successPercent > 60)
        successMessage = "VERY GOOD";
    else if (successPercent > 40)
        successMessage = "GOOD";
    else if (successPercent > 10)
        successMessage = "AVERAGE";
    else if (successPercent!=0&&successPercent <= 10) 
   //needs both condition since successPercent==0 is also
                                     valid successPercent <=10
        successMessage = "BAD";
    else if (successPercent == 0)
        successMessage = "TERRIBLE";

答案 3 :(得分:0)

您可以使用哈希表:

声明一个类变量:

Map<Integer,String> map;

初始化一次:

map = new HashMap<Integer,String>();
map.put( 0,"TERRIBLE");
map.put( 1,"BAD");
map.put( 2,"AVERAGE");
map.put( 3,"AVERAGE");
map.put( 4,"GOOD");
map.put( 5,"GOOD");
map.put( 6,"VERY GOOD");
map.put( 7,"VERY GOOD");
map.put( 8,"EXCELLENT");
map.put( 9,"EXCELLENT");
map.put(10,"OUTSTANDING");

在需要时获取分数字符串:

successMessage = map.get((successPercent-10)/10+1);