-4
{      
       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)在此问题之前,我的代码没有任何问题。有人能告诉我我哪里错了吗

4

2 回答 2

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

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

  • 总现金 * 0 = 0

所以:

  • 总现金 = 0

如果要使用双精度数,还需要在数学表达式中使用双精度数。目前上述值都是整数。所以代替这个:

95/100

做这个:

95.0/100.0
于 2013-11-12T01:59:57.377 回答
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;
于 2013-11-12T01:59:16.133 回答