Java乘以双精度并转换为整数

时间:2015-07-20 07:45:00

标签: java casting integer double

我正在使用Java 7

我正在尝试将两个双精度相乘并将它们转换为整数,如下所示:

double xp = value*multiplier; // These are both doubles
int exp = (int) xp; // I suspect the problem is here
System.out.println("output: "+exp);

当前值为14且乘数为50,但是系统打印出来.21。Java思维14 * 50是21还是导致此转换的双重转换。

我试过把它们放在一起像这样:

int xp = (int) (value*multiplier);

然而,这会产生相同的21输出。

我之前尝试过将一个double转换为int并且它完美无缺。我已经尝试过这种方法,这是我首先向你展示的,它可以在其他地方使用,但不是在这里。

再次:值= 14,乘数= 50

为什么输出会产生,如何修复它以产生正确的数学输出。

注意:您可能会注意到这些数字都没有小数点,使其成为双倍。这些数字可以是小数,具体取决于用户输入。

更多代码:

double multiplier = 1.0;
if(p.getInventory().getItemInHand().hasItemMeta())
{
    if(p.getInventory().getItemInHand().getItemMeta().hasLore())
    {
        if(p.getInventory().getItemInHand().getItemMeta().getLore().contains("§7BlockExperience I"))
        {
            multiplier = main.getConfig().getDouble("expenchlevel1");
        }
        if(p.getInventory().getItemInHand().getItemMeta().getLore().contains("§7BlockExperience II"))
        {
            multiplier = main.getConfig().getDouble("expenchlevel2");
        }
        if(p.getInventory().getItemInHand().getItemMeta().getLore().contains("§7BlockExperience III"))
        {
            multiplier = main.getConfig().getDouble("expenchlevel3");
        }
        if(p.getInventory().getItemInHand().getItemMeta().getLore().contains("§7BlockExperience IV"))
        {
            multiplier = main.getConfig().getDouble("expenchlevel4");
        }
        if(p.getInventory().getItemInHand().getItemMeta().getLore().contains("§7BlockExperience V"))
        {
            multiplier = main.getConfig().getDouble("expenchlevel5");
        }
    }
}

和变量值

int value = main.getConfig().getInt("blockexperience."+key);

这条路径没有错误,我把50放在那里

当打印出两个值时,我的预期值为:14乘数:50.0

2 个答案:

答案 0 :(得分:2)

我怀疑您误解了valuemultiplier的值,或者您的方法存在问题;例如你是如何编译和运行代码的。

  

Java思维14 * 50是21还是导致这种情况的双重转换。

IMO,都没有。

基本上,你的初始代码适用于我(Java 8)....这里是我的LXTerminal会话剪切和粘贴的证据。

[steve@newbox tmp]$ javac Exp.java 
[steve@newbox tmp]$ java Exp 
output: 700
[steve@newbox tmp]$ cat Exp.java 
public class Exp {
    public static void main(String[] args) {
        double value = 14;
        double multiplier = 50;
        double xp = value*multiplier; // These are both doubles
        int exp = (int) xp; // I suspect the problem is here
        System.out.println("output: "+exp);
    }
}
[steve@newbox tmp]$ java -version
openjdk version "1.8.0_45"
OpenJDK Runtime Environment (build 1.8.0_45-b14)
OpenJDK 64-Bit Server VM (build 25.45-b02, mixed mode)
[steve@newbox tmp]$ 

更新 - 查看您发布的其他代码片段(目前为止),我仍然没有看到令人信服的“证据链”来显示value和{{ 1}}拥有你认为他们拥有的价值。

答案 1 :(得分:0)

示例代码:

public class QuickTester {

    public static void main(String[] args) {   

        double value = 14.0;
        double multiplier = 50.0;

        // 1) Set breakpoint here
        double xp = value * multiplier;
        int exp = (int) xp;
        System.out.println("Output (Double): " + exp);      

        // 2) Or set breakpoint here
        int expInt = (int) (value * multiplier);
        System.out.println("Output (Int): " + expInt);
    }
}

<强>输出:

Output (Double): 700
Output (Int): 700

我两个都得到700(使用Java 7) 代码的问题在于其他地方,您需要向我们展示所有相关代码以获得更好的答案。

另一种选择:你可以在进行乘法的代码行设置断点,例如

double xp = value * multiplier;

并使用调试器检查乘法时的值(即值和乘数)。
从那里,你可以缩小问题所在。

相关问题