【问题标题】:Modulo operation will produce no negative numbers模运算不会产生负数
【发布时间】:2018-05-28 11:06:39
【问题描述】:

我在java中有一个计算坐标的小方法。但是如果其中一个坐标是负数,我只会得到一个 0 而不是负数。

private static void highlightIslandBorders(Location loc) {
        World world = loc.getWorld();

        int sx = loc.getBlockX();
        sx -= sx % islandSize;

        int sz = loc.getBlockZ();
        sz -= sz % islandSize;

        if ((sx < 0) || (sz < 0)) {
            return;
        }

        int ex = sx + islandSize - 1;
        int ez = sz + islandSize - 1;

        int y = loc.getBlockY() - 1;

        Material cornerMat = Material.GLOWSTONE;
        world.getBlockAt(sx, y, sz).setType(cornerMat);
        world.getBlockAt(ex, y, sz).setType(cornerMat);
        world.getBlockAt(sx, y, ez).setType(cornerMat);
        world.getBlockAt(ex, y, ez).setType(cornerMat);
    }

【问题讨论】:

  • 它是我的世界模组/插件吗?你想用它做什么?

标签: java modulo


【解决方案1】:

0 到底是从哪里得到的?

int islandSize = 3;
int sx = -100;

System.out.println("Mod: " + (sx % islandSize));

sx -= sx % islandSize;
System.out.println("sx: " + sx);

结果

模组:-1
sx:-99

【讨论】:

  • pic-upload.de/view-35393359/Test.png.html 这是我服务器的控制台
  • 本例中的 islandSize 是什么?另外,你是把 sz 的值打印了两次还是把“sz:”写错了两次?
  • islandSize = 31
  • int islandSize = 31; int sx = 46; System.out.println("Mod: " + (sx % islandSize)); sx -= sx % islandSize; System.out.println("sx: " + sx); 模组:15 sx:31
  • 它对我有用,所以你的代码中可能有一些你没有向我们展示的东西。
猜你喜欢
  • 2015-07-23
  • 2015-08-29
  • 2021-03-20
  • 2011-04-22
  • 2018-01-06
相关资源
最近更新 更多