-1

When i enter amount to buy any fruit the output of fruit quantity is wrong. ex.( if a fruit price is 120 and i enter 60 to buy fruit the output should be 500 Gram but the output showing 480 gram ) First time asking question on stack so i don't no how to write

       int appleprice = 120;
       int bananaprice = 100;
       int orangeprice = 80;
       int mangoprice = 110;
       int grapeprice = 140;
       int lemonprice = 60;
       
       System.out.println("...............................Welcome to Ali Fruit Shop.........................");
       
       System.out.println("\n \n These items are available in our shop :");
       System.out.println("\n \n1.Apples");
       System.out.println("2.Banana");
       System.out.println("3.Orange ");
       System.out.println("4.Mango");
       System.out.println("5.Grapes");
       System.out.println("6.Lemon");
          
       Scanner scanner = new Scanner(System.in);
       
       System.out.println("\n \nEnter Fruit number to buy this fruit :");
       int fruitnuminput = scanner.nextInt();
       
       switch(fruitnuminput)
       
       {
       
       case 1: System.out.println("Apples : Rs."+appleprice+" KG");
       System.out.println("\n \n Enter cash amount to buy Apple : ");
       int applebuyamount = scanner.nextInt();
       double applequantity = 1000/appleprice *applebuyamount;
       System.out.println("You will get :");
       System.out.println("Apples : "+applequantity+"Gram");
       break;
       
       case 2: System.out.println("Banana : Rs."+bananaprice+" Dozen");
       System.out.println("\n \n Enter cash amount to buy Banana : ");
       int bananabuyamount = scanner.nextInt();
       double bananaquantity = 12/bananaprice *bananabuyamount;
       System.out.println("You will get :");
       System.out.println("Banana : "+bananaquantity);
       break;
       
       case 3: System.out.println("Orange : Rs."+orangeprice+" KG");
       System.out.println("\n \n Enter cash amount to buy Orange : ");
       int orangebuyamount = scanner.nextInt();
       double orangequantity = 1000/orangeprice *orangebuyamount;
       System.out.println("You will get :");
       System.out.println("Orange : "+orangequantity+"Gram");
       break;
       
       case 4: System.out.println("Mango : Rs."+mangoprice+" KG");
       System.out.println("\n \n Enter cash amount to buy Mango : ");
       int mangobuyamount = scanner.nextInt();
       double mangoquantity = 1000/mangoprice *mangobuyamount;
       System.out.println("You will get :");
       System.out.println("Mango : "+mangoquantity+"Gram");
       break;
       
       case 5: System.out.println("Grapes : Rs."+grapeprice+" KG");
       System.out.println("\n \n Enter cash amount to buy Grapes : ");
       int grapebuyamount = scanner.nextInt();
       double grapequantity = 1000/grapeprice *grapebuyamount;
       System.out.println("You will get :");
       System.out.println("Grapes : "+grapequantity+"Gram");
       break;
       
       case 6: System.out.println("Lemon : Rs."+lemonprice+" KG");
       System.out.println("\n \n Enter cash amount to buy Lemon : ");
       int lemonbuyamount = scanner.nextInt();
       double lemonquantity = 1000/lemonprice *lemonbuyamount;
       System.out.println("You will get :");
       System.out.println("Lemon : "+lemonquantity+"Gram");
       break;
       default:  
       
       
       
       
       
       
       }
      
   }
}
Jens
  • 67,715
  • 15
  • 98
  • 113
Ali Amin
  • 11
  • 1
  • Time to learn how to debug – Jens Aug 10 '22 at 06:34
  • 3
    I recommend reading [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for tips on debugging your code. – Code-Apprentice Aug 10 '22 at 06:34
  • 1
    try this question/answer [Why does integer division code give the wrong answer?](https://stackoverflow.com/q/7286681/16320675) or [Int division: Why is the result of 1/3 == 0?](https://stackoverflow.com/q/4685450/16320675) - your title is not very precise, I doubt you really just wan to know if "is anyone there who can solve..." – user16320675 Aug 10 '22 at 07:03

1 Answers1

1

Integer division.

If you do 5/2 in java, the result is 2 instead of 2.5, because it treats the result as an integer rather than a floating-point number.

To solve it, an easy fix is cast it: for example, 1000.0/appleprice * applebuyamount or 1000/(double)appleprice * applebyamount in the apple case. Changing one literal/variable in the division expression is sufficient.

MMZK1526
  • 654
  • 2
  • 16