首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对于一个特定的数字,我有3种不同的算法,我不知道如何使它们正常工作

对于一个特定的数字,我有3种不同的算法,我不知道如何使它们正常工作
EN

Stack Overflow用户
提问于 2022-04-26 10:43:41
回答 2查看 39关注 0票数 0

我正在修Java课程,他们给我们做了一个项目。我正在为一些要求而奋斗,我正在努力理解它们。

findTreause (int treasureCode):该方法将根据发现的宝藏更新玩家的状态,如下所示:

如果treasureCode是完美数字:

得分增加5000分

寿命增加1岁

健康变成100。

如果treasureCode是素数:

得分增加1000分

生命增加2个生命

健康增长25,但不能超过100

如果treasureCode是偶数,其数字之和可被3除,那么该对象就是陷阱。因此,球员的情况变化如下:

得分减少3000分

健康下降了25。

如果健康达到0,那么生命将减少1,并且将健康重置为100,任何其他数字都不会显示“魔力”,因此唯一的状态更新是:

分数将由treasureCode增加

注:先前的标准将逐个测试(即按给定的顺序),状态将根据第一个验证条件进行更新。例如:如果一个treasureCode既是一个“完全数”,又是一个“偶数,其数字之和可被3整除”,则状态将根据“完全数”进行更新,因为该标准先于另一个标准。

我已经做了完美的素数算法,但是当我输入数字6,也就是一个完美数时,质数也会增加。

代码语言:javascript
运行
复制
package game;

public class PlayerStatus {
    
    public void findArtifact(int artifactCode) {
        int sum = 1;
        for (int i = 2; i < artifactCode; i++) {
            if (artifactCode % i == 0) {
                sum += i;
            }
        }
        if (sum == artifactCode) {
            this.score += 5000;
            this.lives += 1;
            this.health = 100;
        }
        for (int i = 2; i < artifactCode / 2; i++) {
            if (artifactCode % i == 0) {
                break;
            }
        }
        this.score += 1000;
        this.lives += 2;
        this.health += 25;
     }

package game;

public class Main {

    public static void main(String[] args) {
    PlayerStatus player1 = new PlayerStatus();
        player1.findArtifact(6);
        System.out.println(player1.getScore());
        System.out.println(player1.getLives());
        System.out.println(player1.getHealth());

这是输出:

  • 6000
  • 3
  • 125

我知道这很容易,但我有精神崩溃。有人能解释一下我要做什么吗?

EN

回答 2

Stack Overflow用户

发布于 2022-04-26 11:03:58

在一个单独的if中是否有每个条件?如果你这样做了,它将评估每一种情况分别,并将改变你的字符统计。为了做你想做的事,接下来的条件如下:

代码语言:javascript
运行
复制
if(perfect) {
  do somehting;
} else if(prime) {
  do something;
} else if(even and sum of digits...) {
  do something;
} else {
  do something if nothing matches criteria;
}
票数 0
EN

Stack Overflow用户

发布于 2022-04-26 13:46:26

我设法解决了我的问题:

代码语言:javascript
运行
复制
public static boolean isPerfect(int artifactCode) {
        int sum = 0;
        for (int i = 1; i <= artifactCode / 2; i++) {
            if (artifactCode % i == 0) {
                sum += i;
            }
        }
        if (sum == artifactCode) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isPrime(int artifactCode) {

        for (int i = 2; i <= artifactCode / 2; i++) {
            if (artifactCode % i == 0)
                return false;
        }
        return true;
    }

    public static boolean sumOfDigits(int artifactCode) {
        int sum = 0;
        int lastDigit = 0;
        while (artifactCode > 0) {
            lastDigit = artifactCode % 10;
            sum = sum + lastDigit;
            artifactCode /= 10;
        }
        if (sum % 3 == 0) {
            return true;
        } else {
            return false;
        }
    }

    public void findArtifact(int artifactCode) {
        if (isPerfect(artifactCode)){
            this.score += 5000;
            this.lives += 1;
            this.health = 100;
        } else if (isPrime(artifactCode)) {
            this.score += 1000;
            this.lives += 2;
            this.health += 25;
        } else if (sumOfDigits(artifactCode) && artifactCode % 2 == 0) {
            this.score -= 3000;
            this.health -= 25;
        } else {
            this.score = artifactCode;
        }

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72012589

复制
相关文章

相似问题

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