任何人都可以在我的这个java代码中发现错误

时间:2013-11-12 01:52:39

标签: java

{      
       PrintHeaderLine();

       System.out.format("%-15s %-55s %15s %n", "SL NO", "PRODUCT", "COST");

       PrintHeaderLine();

      for(int x=0;x<k;x++)
      {
          totalcash=totalcash+amount[x];
          System.out.format("%-15s %-55s %15s %n", (x+1), itemsbought[x], "$"+ amount[x]);
      }      
      if(totalcash>=500&&totalcash<1000)
      {
          totalcash=totalcash*(95/100);
        }
      else if(totalcash>=1000&&totalcash<3000)
      {
      totalcash=totalcash*(90/100);
    }
      else if(totalcash>=3000)
      {
      totalcash=totalcash*(85/100);

    }
    else 
      totalcash=totalcash+0;
      PrintHeaderLine();     

      System.out.format("%-15s %71s %n", "GRAND TOTAL ", "$" + totalcash);

(我保持输出为0)只有这个问题之前,我的代码并没有错误。有人可能会告诉我哪里错了

2 个答案:

答案 0 :(得分:1)

在if / if else语句中,您将totalcash变量乘以整数值0.当您将整数(95)除以另一个整数(100)时,您会得到整数除法,这相当于整数值为0。

尝试:

if(totalcash>=500&&totalcash<1000)
{
    totalcash=totalcash*((double)95/100);
}
else if(totalcash>=1000&&totalcash<3000)
{
    totalcash=totalcash*((double)90/100);
}
else if(totalcash>=3000)
{
    totalcash=totalcash*((double)85/100);
}
else 
     totalcash=totalcash+0;

答案 1 :(得分:1)

  • 95/100 = 0
  • 90/100 = 0
  • 85/100 = 0

鉴于所有这些都等于0,那么:

  • totalcash * 0 = 0

因此:

  • totalcash = 0

如果你想使用双打,你也需要在数学表达式中使用双打。目前,上述值均为整数。所以不要这样:

95/100

这样做:

95.0/100.0