开发者

Comparing two numbers

I have this program piece:

double stockWeight = 1;
    if(data[0] > 9 )        
        stockWeight = 1000*R(data[0]/10);
    double compare = data[0]*100-stockWeight;
    System.out.println(compare);
    if(compare > 300.0 && compare <=600.0)
        stockWeight += 300;
    else if(compare > 600.0 && compare <= 900.0)
        stockWeight += 600;
    else if(compare > 900.0);
        stockWeight += 900;

        System.out.println(stockWeight);

 ///////////////////////////////////
   private int R(double D){
      int howBIG = 1;
      if (D >= 100){howBIG = 10;}
      else if(D >= 1000){howBIG = 100;}
      DecimalFormat F = new DecimalFormat("#");
      return Integer.valueOf(F.format(D/howBIG))*howBIG;
  }

the output is:

126.0
1900.0

All numbers here are double type. data[0] = 11.26 why does my computer think that 126.0 is开发者_如何转开发 greater than 900.0?? R method basically is div function of 10(100) if greater than 100(1000), gives whole number


You are likely seeing the confusing result because of the stray semicolon in your last else statement:

else if(compare > 900.0);

So this line doesn't depend on the else clause and will always be executed:

stockWeight += 900;

Essentially you will always add 900 to the stockWeight immediately before the last print statement, whether or not compare > 900.0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜