java if then语句

时间:2011-03-23 15:12:01

标签: java

我希望程序检查需要多少个大盒子,然后使用剩余部分来确定需要多少个中型盒子,并对小盒子做同样的事情。 但是当我运行这个

确定大框的第一步

变量pensnotinbox 即使我知道应该有一个余数

,也会出现0
   pen.setName("Awesome Pen");
     pen.setPrice(5.0);
     pen.setUPC(102);
     pen.setMediumCapacity(50);
     pen.setLargeCapacity(100);

     //printing information about the product to the user, and asking how many of this item they want to purchase
     System.out.print("Product 2 \n"   + "Name:" + pen.getName() + "    Price: " + pen.getPrice() + "    UPC: " + pen.getUPC() + "\n");
     System.out.print("How Many Of These Would You Like To Purchase?\n" );
     //using the scanner to get the integer/quantity they want
     penquant = scan.nextInt();
      //storing the total price in a variable 

        int penlargeboxes;
        double penboxes;
        int penmediumboxes;
        double penremainder;
        double pensnotinbox;
         int pensmallboxes;

      if (pen.getLargeCapacity() <= penquant) {
          penlargeboxes = penquant/pen.getLargeCapacity(); 
          penremainder = penquant % pen.getLargeCapacity(); 


          System.out.print(penlargeboxes + "\n");
          System.out.print(penremainder + "\n");


           if (penremainder > 0 ) {

              penboxes = penremainder/pen.getMediumCapacity() ;
              penmediumboxes = ((int)penboxes);
              penremainder =  penquant % pen.getLargeCapacity(); 

              pensnotinbox = penremainder;

              System.out.print(penmediumboxes + "\n");
              System.out.print(pensnotinbox + "\n");

            }else {
                if (penremainder > .99 ) {

               penboxes = penremainder/1 ;
               pensmallboxes = ((int)penboxes);

                  System.out.print(pensmallboxes + "\n");


            }

            }

     } else {
          System.err.println("OOPS!");
     } 


     pentotal= (pen.totalPurchase(penquant));
     //printing their total cost for this item
     System.out.print("The total cost for this item will be " + pentotal + "\n" + "\n");

6 个答案:

答案 0 :(得分:3)

penquantpen.getLargeCapacity()有哪些类型?如果它们都是整数,则表示正在执行整数除法,它没有小数分量(余数被丢弃)。然后整数结果将被提升为double。

如果是这种情况,你可以尝试

penboxes = ((double)penquant)/pen.getLargeCapacity();

答案 1 :(得分:2)

我想,在您的代码中,penquantgetLargeCapacity()都是整数。所以除法的结果也是整数。 试试,

penboxes = penquant/((double)pen.getLargeCapacity());

penboxes = ((double)penquant)/pen.getLargeCapacity();

答案 2 :(得分:2)

正如@Mark Peters指出的那样,问题很可能是penquantpen.getLargeCapacity()都是整数,这意味着java正在进行整数除法,然后将整数结果转换为double。他发布的替代修复方法是更改​​行:

penboxes = penquant/pen.getLargeCapacity(); 
penlargeboxes = ((int)penboxes);
penremainder = (penboxes-(int)penboxes );

pensnotinbox=penremainder*pen.getLargeCapacity();

利用内置整数除法和模运算符并完全消除步骤。修改后的代码如下所示:

penlargeboxes = penquant/pen.getLargeCapacity(); 
pensnotinbox = penquant % pen.getLargeCapacity(); 

答案 3 :(得分:0)

看看这部分:

penremainder = (penboxes-(int)penboxes );

pensnotinbox = (penremainder * pen.getLargeCapacity());

在第一行之后,penremainer必须为0.然后你将0与pen.getLargeCapactiy()相乘。结果也必须为0。

答案 4 :(得分:0)

你的第一个条件是:

 if (pen.getLargeCapacity() <= penquant) ...

但是什么

 if (pen.getLargeCapacity() > penquant) ...

???

答案 5 :(得分:-2)

penremainder = (penboxes-(int)penboxes );

此行始终等于零,您将从其自身减去笔盒。