开发者

java if then statement

I want the program to check how many large boxes are needed, then use the remainder to determine how many medium boxes are needed and do the same for small boxes. But when i run this

in the first step to determine the large boxes

variable pensnotinbox comes out as 0 even though i know there should be a remainder

   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 t开发者_运维知识库hey 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");


What are the types of penquant and pen.getLargeCapacity()? If they are both integers, you are performing integer division which has no fractional component (the remainder is discarded). The integer result would then be promoted to a double.

So if that's the case you can instead try

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


I guess, in your code, both penquant and getLargeCapacity() are integer. so the result of division is also integer. try like,

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

or

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


As @Mark Peters noted, the issue is most likely that penquant and pen.getLargeCapacity() are both integers meaning java is doing integer division and then casting the integer result to a double. An alternative fix to the one he posted is to change the lines:

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

pensnotinbox=penremainder*pen.getLargeCapacity();

to take advantage of built in integer division and the modulo operator and eliminate a step altogether. The modified code would look like this:

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


Look at this part:

penremainder = (penboxes-(int)penboxes );

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

After the first line, penremainer has to be 0. Then you're multiplying 0 with pen.getLargeCapactiy(). The result has to be 0 too.


Your first condition states:

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

But what

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

???


penremainder = (penboxes-(int)penboxes );

This line will always equal zero, you are subtracting penboxes from itself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜