如果double是int,则测试java

时间:2016-08-20 10:11:37

标签: java int double equals-operator

我的部分程序测试两个数字是否相等。因为某些操作只需要双打,而其他操作只需要整数,我会比较一次和两次。程序正在返回,即使它们只是 round 相等(即7.5=7),两者是相等的。我只希望程序返回true,如果两个实际 相等。我已尝试过此处列出的解决方案:How to test if a double is an integer 判断我的双打是否是整数 所有这些似乎都有效 - 它们会编译,但程序仍会返回7=7.5 我也尝试过另一个方向 - 将我的整数转换成双打 - 同样的结果。如何让我的程序承认差异?最新的建议:

import acm.program.ConsoleProgram;
import java.awt.Color;
import  acm.io.IODialog;
import java.text.*;
import static java.lang.Math.*;
import java.util.*;

/** Tests to see if user color matches sample colors */
public class ColorMatch extends ConsoleProgram
{
//defining sample colors
Color[] dmc =
{
    new Color(255,255,255),
    new Color(43,57,41),
    new Color(213,39,86),
    new Color(0,160,130),
    new Color(0,0,0),
};

public void run()
{
    average();

}

//averages three colors, then tests for match to given color
public void average()
{
    //asks for stitch color
    IODialog dialog     = new IODialog();
    int stitchRed       = dialog.readInt("Enter red value: ");
    int stitchGreen     = dialog.readInt("Enter green value: ");
    int stitchBlue      = dialog.readInt("Enter blue value: ");
    Color stitchColor   = new Color(stitchRed,stitchGreen,stitchBlue);

    //gets averages for dmc colors
    for (Color i:dmc)
    {
        for (Color j:dmc)
        {
            for (Color k:dmc)
            {
                int indexI = Arrays.asList(dmc).indexOf(i);
                int indexJ = Arrays.asList(dmc).indexOf(j);
                int indexK = Arrays.asList(dmc).indexOf(k);
                if  (indexI <= indexJ && indexJ <= indexK)
                {
                    int iRed = i.getRed();
                    int jRed = j.getRed();
                    int kRed = k.getRed();
                    int iGreen = i.getGreen();
                    int jGreen = j.getGreen();
                    int kGreen = k.getGreen();
                    int iBlue = i.getBlue();
                    int jBlue = j.getBlue();
                    int kBlue = k.getBlue();
                    double redAverage   = (iRed+jRed+kRed)/3;
                    double greenAverage = (iGreen+jGreen+kGreen)/3;
                    double blueAverage  = (iBlue+jBlue+kBlue)/3;
                    if (redAverage == (int)redAverage && greenAverage == (int)greenAverage && blueAverage == (int)blueAverage)
                    {
                        int rAverage = (int)redAverage;
                        int gAverage = (int)greenAverage;
                        int bAverage = (int)blueAverage;
                        Color colorAverage = new Color(rAverage,gAverage,bAverage);

                        //tests to see if any average equals the stitch color
                        if (colorAverage.equals(stitchColor))
                        {
                            println("The color match is: " + i + ", " + j + ", " + k);
                        }
                    }
                }
            }
        }
    }

我插入了85s作为我的测试号码 唯一的结果应该是(0,0,0)+(0,0,0)+(255,255,255),但它也会产生(43,57,41)+(213,39,86)+(0,160,130) . (41+86+130)/3=85.7!=85

1 个答案:

答案 0 :(得分:0)

我认为问题在于你正在比较&#39; int&#39;并且&#39; double&#39;将double值转换为int后截断小数部分的值。 例如,(7==7.5) false ,但(7==(int)7.5) true ,因为(int)7.5 = 7。 因此,如果您想要int和double之间的严格比较,您可以直接比较它们而不需要强制转换。如果您想了解有关如何将int转换为int的更多信息,请参阅How does double to int cast work in Java