给定浮点值,可能有损转换从double到float? [英] Possible lossy conversion from double to float, given float values?

查看:117
本文介绍了给定浮点值,可能有损转换从double到float?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没弄错的话,0.5是十进制数字;因此,使其成为浮动值。但是为什么java告诉我它是双重的?返回语句被java检测为错误,说:不兼容的类型:从double到float的可能有损转换

if i'm not mistaken, "0.5" are decimal numbers; thus, making it a float value. but why is java telling me that it is a double?. the return statements are detected as errors by java, saying: "incompatible types:possible lossy conversion from double to float"

public float typeDmgMultiplr(String type,String type2){
        if(type.equalsIgnoreCase("grass")){
            if(type2.equalsIgnoreCase("grass"))
                return 0.5;
            else if(type2.equalsIgnoreCase("poison"))
                return 0.5;
            else if(type2.equalsIgnoreCase("fire"))
                return 0.5;
            else
                return 2.0;
        }
        else if(type.equalsIgnoreCase("fire")){
            if(type2.equalsIgnoreCase("grass"))
                return 2.0;
            else if(type2.equalsIgnoreCase("poison"))
                return 1.0;
            else if(type2.equalsIgnoreCase("fire"))
                return 0.5;
            else
                return 0.5;
        }
        else if(type.equalsIgnoreCase("water")){
            if(type2.equalsIgnoreCase("grass"))
                return 0.5;
            else if(type2.equalsIgnoreCase("poison"))
                return 1.0;
            else if(type2.equalsIgnoreCase("fire"))
                return 2.0;
            else
                return 0.5;
        }
        else{
            if(type2.equalsIgnoreCase("grass"))
                return 2.0;
            else if(type2.equalsIgnoreCase("poison"))
                return 0.5;
            else if(type2.equalsIgnoreCase("fire"))
                return 1.0;
            else
                return 1.0;
        }

    }


推荐答案

您有两种选择。

一种方法是将方法的返回类型更改为double。

One is change your method's return type to double.

其他正如许多人在评论中所说的那样,将更改返回浮点值的double值。

The other is change the double values you are returning to float values, as many have said in the comments.

public float typeDmgMultiplr(String type,String type2){
    if(type.equalsIgnoreCase("grass")){
        if(type2.equalsIgnoreCase("grass"))
            return 0.5f;
        else if(type2.equalsIgnoreCase("poison"))
            return 0.5f;
        else if(type2.equalsIgnoreCase("fire"))
            return 0.5f;
        else
            return 2.0f;
    }
    else if(type.equalsIgnoreCase("fire")){
        if(type2.equalsIgnoreCase("grass"))
            return 2.0f;
        else if(type2.equalsIgnoreCase("poison"))
            return 1.0f;
        else if(type2.equalsIgnoreCase("fire"))
            return 0.5f;
        else
            return 0.5f;
    }
    else if(type.equalsIgnoreCase("water")){
        if(type2.equalsIgnoreCase("grass"))
            return 0.5f;
        else if(type2.equalsIgnoreCase("poison"))
            return 1.0f;
        else if(type2.equalsIgnoreCase("fire"))
            return 2.0f;
        else
            return 0.5f;
    }
    else{
        if(type2.equalsIgnoreCase("grass"))
            return 2.0f;
        else if(type2.equalsIgnoreCase("poison"))
            return 0.5f;
        else if(type2.equalsIgnoreCase("fire"))
            return 1.0f;
        else
            return 1.0f;
    }

}

这篇关于给定浮点值,可能有损转换从double到float?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆