cannot implicitly convert type 'float' to 'int'

前端 未结 2 1010
滥情空心
滥情空心 2021-01-29 15:35

When I try to run the following code it gives an error which is \" Cannot implicitly convert type \'float\' to \'int\' \".

I have done a lot of searches, and sadly I was

相关标签:
2条回答
  • 2021-01-29 16:03

    Since h, twoEnds, and x are all floats, the following equation will return a float:

    sum1 = (h - ((cols) * x) + twoEnds)) / (cols + 1) 
    

    You can either change sum1 to be a float, or cast your equation to be an int like so:

    sum1 = (int)((h - ((cols) * x) + twoEnds)) / (cols + 1));
    
    0 讨论(0)
  • 2021-01-29 16:23

    The errors puts it clear

    cannot implicitly convert type 'float' to 'int'

    So you have a float result which can't be converted implictly to sum1 which is of type int. Try converting/casting it explicitly:

     sum1 = (int) ((h - ((cols) * x) + twoEnds)) / (cols + 1));
    
    0 讨论(0)
提交回复
热议问题